369. Plus One Linked List

Given a non-negative integer represented as non-empty a singly linked list of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.

Example:
Input:
1->2->3
Output:
1->2->4

Solution1:递归 (回传进位)

思路: 递归来达到从后面处理的效果,post处理回传进位。
实现1a/1b: 创建dummy or not (basically same)
Time Complexity: O(N) Space Complexity: O(N) 递归缓存

Solution2:Iterative

思路: // dummy -> 1 -> 2 -> 3 -> 9 -> 9 -> null
如果最后一位不是9,直接+1即可;如果是9,就需要:
找到最后一个不是9的位置,+1,然后后续都置零。
Time Complexity: O(N) Space Complexity: O(1)

Solution1 Code:

class Solution1a {
    public ListNode plusOne(ListNode head) {
        if(DFS(head) == 0) {
            return head;
        }else{
            ListNode newHead = new ListNode(1);
            newHead.next = head;
            return newHead;
        }
    }

    public int DFS(ListNode head){
        if(head == null) return 1;   // last one: plus one

        // recursion
        int carry = DFS(head.next);

        // deal with current level
        if(carry == 0) {
            return 0;
        }
        else {
            int val = head.val + carry;
            head.val = val % 10;
            return val / 10;
        }
    }
}

Solution1b Code:

class Solution1b {
    public ListNode plusOne(ListNode head) {
        
        // dummy head for potential carry;
        ListNode dummy = new ListNode(0); 
        dummy.next = head;
        
        DFS(dummy);
        if(dummy.val == 0) return dummy.next;
        else return dummy;
     
    }

    public int DFS(ListNode head){
        if(head == null) return 1;   // last one: plus one

        // recursion
        int carry = DFS(head.next);

        // deal with current level
        if(carry == 0) {
            return 0;
        }
        else {
            int val = head.val + carry;
            head.val = val % 10;
            return val / 10;
        }
    }
}

Solution2 Code:

class Solution2 {
    public ListNode plusOne(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode i = dummy; // last digit which is not 9
        ListNode j = dummy; // iterating pointer
        
        // dummy  ->  1  ->  2  ->  3  ->  9  ->  9  ->  null
        //                          i             j
        
        // find i: the last digit which is not 9
        while (j.next != null) {
            j = j.next;
            if (j.val != 9) {
                i = j;
            }
        }
        
        // if last one is not 9, then just adding one should work
        if (j.val != 9) {
            j.val++;
        // if last one is nine, then make 399 to 400
        } else {
            i.val++;
            i = i.next;
            while (i != null) {
                i.val = 0;
                i = i.next;
            }
        }
        
        // check if first digit
        if (dummy.val == 0) {
            return dummy.next;
        }
        
        return dummy;
    }
}

你可能感兴趣的:(369. Plus One Linked List)