369. Plus One Linked List
https://leetcode.com/problems/plus-one-linked-list/description/
Input: [1,2,3]
Output: [1,2,4]Thoughts
Code
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def plusOne(self, head: ListNode) -> ListNode:
i, j = None, head
while j:
if j.val != 9:
i = j
j = j.next
if not i:
new_h = ListNode(1)
new_h.next = head
i = head
head = new_h
else:
i.val += 1
i = i.next
while i:
i.val = 0
i = i.next
return head
Analysis
Last updated