Here is the code:
def plusOne(self, head): """ :type head: ListNode :rtype: ListNode """ start = None node = head while node: if node.val < 9: start = node node = node.next if start: start.val += 1 node = start.next else: new = ListNode(1) new.next = head node = head head = new while node: node.val = 0 node = node.next return head
recursive method is not O(1) space. You are virtually using a stack built by the system. And the stack is O(n).
@JackDavid135 The solution posted here is an iterative solution. It uses no extra space.
@luofei The iterative procedure in the system uses stack itself. Please learn some Assembly & OS basics before talking back to me.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.