Leetcode - Task Scheduler
Leetcode - Remove Duplicates from Sorted List II

Leetcode - Plus One Linked List

violet posted @ Aug 11, 2020 02:19:48 AM in 算法 with tags Algorithm java LinkedList , 10615 阅读

https://leetcode.com/problems/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]

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode plusOne(ListNode head) {
        head = reverse(head);
        int carry = 1;
        ListNode node = head;
        ListNode prev = null;
        while(node != null) {
            int sum = node.val + carry;
            carry = 0;
            if (sum > 9) {
                carry = 1;
                sum -= 10;
            }
            node.val = sum;
            prev = node;
            node =  node.next;
        }
        if (carry == 1) {
            prev.next = new ListNode();
            prev.next.val = 1;
        }
        return reverse(head);
    }
    public ListNode reverse(ListNode head) {
        ListNode prev = null;
        ListNode node = head;
        while (node != null) {
            ListNode next = node.next;
            node.next = prev;
            prev = node;
            node = next;
        }
        return prev;
    }
}
Escorts in Chennai 说:
Sep 17, 2021 03:47:00 PM

I truly prefer to peruse a particularly mind-blowing blog. Thank you so much! keep having a good time.

Chennai Escorts Service Mambakkam Escorts Escorts Service in Vellore Alwarpet Escorts Service T Nagar Escort Escorts in Besant Nagar

Bangalore Escorts@gm 说:
Nov 06, 2021 08:43:09 PM

I'm unfathomably enchanted with your article. I love the way wherein you structure this article. Appreciative to you for allowing this stunning post to us.
<a href="http://www.a1delhiescort.in/excited-to-have-fun-in-bangalore.html">Bangalore Call Girls</a>
<a href="http://www.sonalisharma.co.in/bangalore-escorts.html">Bangalore Escorts Service</a>
<a href="http://www.yaina.in/bangalore-call-girls.html">Bangalore Escort</a>
<a href="https://www.jaipur-escort-service.com/bangalore-call-girls.html">Escort in Bangalore</a>
<a href="http://www.alyceescorts.com/bangalore-escorts.html">Call Girl in Bangalore</a>
<a href="http://www.aliyamumbaiescort.in/bangalore-escorts.html">Bangalore Call Girl</a>
<a href="http://www.janhavi.in/bangalore-escorts.html">Bangalore Escort Service</a>
<a href="https://www.ritudas.in/bangalore-escorts.html">Independent Bangalore Escorts</a>
<a href="http://www.riya-panjaban.in/escorts-in-bangalore.html">Independent Escorts in Bangalore</a>
<a href="http://www.thedelhiescortsservice.com/bangalore.html">High Profile Call Girls in Bangalore</a>
<a href="http://www.archanaa.in/escorts-location/bangalore-escorts.html">Independent Call Girls in Bangalore</a>
<a href="http://www.escortsservicesjaipur.in/bangalore-escorts.html">High Profile Escorts in Bangalore</a>
<a href="http://www.jaipurescorts.co.in/bangalore-call-girls.html">Independent Call Girl in Bangalore</a>
<a href="http://www.callgirlsindelhi.net/escorts-in-bangalore.html">Bangalore Escorts</a>
<a href="http://www.escortdehradun.com/bangalore-call-girls.html">Escorts in Bangalore</a>
<a href="https://www.mumbaiescortspriya.com/bangalore-call-girls.html">Call Girls in Bangalore</a>

Paharganj Escorts 说:
Jan 04, 2022 01:27:32 PM

https://www.ncrcallgirls.com/saket-escorts.html
https://www.ncrcallgirls.com/nehru-place-escorts.html
https://www.ncrcallgirls.com/vasant-vihar-escorts.html
https://www.ncrcallgirls.com/
https://ncrcallgirldelhi.wordpress.com/blog/
https://sharmasanvi350.wixsite.com/ncrcallgirls
https://61c9ab8220851.site123.me/
https://ncrcallgirls0.yolasite.com/blog/
https://ncrcallgirlsblog.jimdofree.com/


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter