加一链表(快慢指针 or Stack or Recursive)

https://www.lintcode.com/problem/plus-one-linked-list/description?_from=ladder&&fromId=18

描述

ENG

给定一个非负整数,这个整数表示为一个非空的单链表,每个节点表示这个整数的一位。返回这个整数加一。

除了0本身,所有数字在最高位前都没有0。

列表的头节点存的是这个整数的最高位。

您在真实的面试中是否遇到过这个题?是

题目纠错

样例

样例1

输入: 1 -> 2 -> 3 -> null

输出: 1 -> 2 -> 4 -> null

解释:

123 + 1 = 124

样例2

输入: 9 -> 9 -> null

输出: 1 -> 0 -> 0 -> null

解释:

99 + 1 = 100

Analysis:

这道题有非常多种解法

解法1: 最直接的方式1.reverse linkedlist; 2.add 1; 3.reverse back linkedlist

public ListNode plusOne(ListNode head) {

        // Write your code here

        if (head == null) {

            return null;

        }

        head = reverse(head);

        ListNode p = head;

        int flag = 1;

        while (p != null) {

            int val = p.val + flag;

            p.val = val % 10;

            flag = val / 10;

            p = p.next;

        }

        head = reverse(head);

        if (flag != 0) {

            ListNode node = new ListNode(1);

            node.next = head;

            head = node;

        }

        return head;

    }

    private ListNode reverse(ListNode head) {

        if (head == null) {

            return null;

        }

        ListNode p = head;

        while (p.next != null) {

            ListNode q = p.next;

            p.next = q.next;

            q.next = head;

            head = q;

        }

        return head;

    }

解法2: 创建dummy node(为全为9999的情况做准备)通过slow,fast指针,slow指针最终指向最后一个不是9的node。接着把slow之后包括slow的node都加1(后面其实都是9999...)。如果dummy node的val为1说明需要dummy node作为head返回。

public ListNode plusOne(ListNode head) {

        // Write your code here

        ListNode dummy = new ListNode(0);

        dummy.next = head;

        ListNode slow = dummy, fast = dummy;

        while (fast != null) {

            if (fast.val != 9) {

                slow = fast;

            }

            fast = fast.next;

        }

        while (slow != null) {

            slow.val = (slow.val + 1) % 10;

            slow = slow.next;

        }

        if (dummy.val == 0) {

            dummy = dummy.next;

        }

        return dummy;

    }

解法3: 用stack。先把所有node入栈,如果当前栈顶node为9则变为0并且出栈。如果栈此时为空,则需要创建新的head node并且val为1返回。如果不为空,只需要把栈顶node的val加1返回head即可。

public ListNode plusOne(ListNode head) {

        // Write your code here

        Stack stack = new Stack<>();

        ListNode p = head;

        while (p != null) {

            stack.push(p);

            p = p.next;

        }

        while (!stack.isEmpty() && stack.peek().val == 9) {

            stack.peek().val = 0;

            stack.pop();

        }

        if (!stack.isEmpty()) {

            stack.peek().val += 1;

            return head;

        }

        ListNode dummy = new ListNode(1);

        dummy.next = head;

        head = dummy;

        return head;

    }

解法4: 用Recursive。和解法3类似但是代码会更优雅。

public ListNode plusOne(ListNode head) {

        // Write your code here

        int carry = helper(head);

        if (carry == 1) {

            ListNode newHead = new ListNode(1);

            newHead.next = head;

            return newHead;

        }

        return head;

    }

    private int helper(ListNode node) {

        if (node == null) {

            return 1;

        }

        int carry = helper(node.next);

        int sum = carry + node.val;

        node.val = sum % 10;

        return sum / 10;

    }

你可能感兴趣的:(加一链表(快慢指针 or Stack or Recursive))