:type head: ListNode :rtype: ListNode
(self, head)
| 7 | |
| 8 | class 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 |