MCPcopy Create free account
hub / github.com/qiyuangong/leetcode / plusOne

Method plusOne

python/369_Plus_One_Linked_List.py:9–35  ·  view source on GitHub ↗

:type head: ListNode :rtype: ListNode

(self, head)

Source from the content-addressed store, hash-verified

7
8class Solution(object):
9 def plusOne(self, head):
10 """
11 :type head: ListNode
12 :rtype: ListNode
13 """
14 dummy = ListNode(0)
15 dummy.next = head
16 place_stop, tail = dummy, dummy
17 # find the tail
18 while tail.next is not None:
19 tail = tail.next
20 if tail.val != 9:
21 place_stop = tail
22 if tail.val != 9:
23 # done
24 tail.val += 1
25 else:
26 # not yet
27 place_stop.val += 1
28 place_stop = place_stop.next
29 # set all node behind this place to zero
30 while place_stop is not None:
31 place_stop.val = 0
32 place_stop = place_stop.next
33 if dummy.val == 0:
34 return dummy.next
35 return dummy

Callers

nothing calls this directly

Calls 1

ListNodeClass · 0.70

Tested by

no test coverage detected