zoukankan      html  css  js  c++  java
  • leetcode369- Plus One Linked List- medium

    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

    和+1数组有点像,关键要知道最多只要记录一串X9999就够了,前面的都是浮云,如果出现小于9的数字就相当于屏障,再也影响不到它之前的了。

    最后这串被记录下来的,每个digit都做 digit = (digit + 1) % 10的操作。如果最前面的X也是9,那说明其实你整串肯定都是9999999,否则如89999不可能会最后存下来的第一个是9的。这个时候特殊还要再加一个1在最前头。

    关于怎么记录有两种方法:

    1.O(n) 空间:用一个stack。存下来后一个个吐回去

    2.O(1)空间:用一个ListNode,只要记下最前面的X即可, 因为可以调用next不断向后遍历。

    细节:1.小心99999多加一个点的问题。

    实现1,用stack:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode plusOne(ListNode head) {
            Stack<ListNode> stack = new Stack<>();
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            while (head != null) {
                if (head.val != 9) {
                    stack.clear();
                }
                stack.push(head);
                head = head.next;
            }
            ListNode crt = null;
            while (!stack.isEmpty()) {
                crt = stack.pop();
                crt.val = (crt.val + 1) % 10;
            }
            if (crt != null && crt.val == 0) {
                ListNode newN = new ListNode(1);
                newN.next = dummy.next;
                dummy.next = newN;
            }
            return dummy.next;
        }
    }

    实现2,用指针:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode plusOne(ListNode head) {
            Stack<ListNode> stack = new Stack<>();
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode change = head;
            
            // find the start point of those node that need change
            while (head != null) {
                if (head.val != 9) {
                    change = head;
                }
                head = head.next;
            }
            
            // for the special case of 99999999...
            if (change.val == 9) {
                ListNode newN = new ListNode(1);
                newN.next = change;
                dummy.next = newN;
            }
            
            // make the add one change
            while (change != null) {
                change.val = (change.val + 1) % 10;
                change = change.next;
            }
            
            return dummy.next;
        }
    }
  • 相关阅读:
    Linux Docker容器磁盘出现日志/var/lib/docker/overlay2占用100%
    (转)关于文字放大缩小
    Jquery 点击按钮切换div(可循环轮播)
    C#调用C++的dll两种方法(托管与非托管)
    Spring——项目优雅停机
    Maven——(mavenantrunplugin、mavendependencyplugin)插件的使用
    微信开放平台第三方平台开发的一些坑解决以备忘 编程
    QRCodeEncoder 生成二维码提示 数组越界 的解决方法 编程
    jenkins+docker+nginx+vue前端项目实现自动部署
    nginx+vue项目请求后端接口报错:405 not allowed
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7976841.html
Copyright © 2011-2022 走看看