[Leetcode] Plus one Linked list linked list plus an operation

Source: Internet
Author: User

Given a non-negative number represented as a singly linked list of digits, plus one to the number.

The digits is stored such, the most significant digit was at the head of the list.

Example:

Input:1->2->3output:1->2->4

This problem gives us a linked list, used to simulate a three-digit number, the table head is high, now let us add 1 operation, the difficulty of this problem is that the list can not be accessed through the coordinates of the elements, only through the way of traversal, and this problem just let us start from the end of the chain, from the back to the forward, encountered the At the end of the day it may be necessary to make up a bit. Then we think, if the end of the chain is high, then add 1 operation is more convenient, directly on the side of the side of the operation of the processing, then we can do is to turn the list first, and then now is the chain tail is high, we do add 1 processing after the end, and then flip the list back can be, See the code below:

Solution One:

classSolution { Public: ListNode* PlusOne (listnode*head) {        if(!head)returnHead; ListNode*rev_head = Reverse (head), *cur = rev_head, *pre =cur; intcarry =1;  while(cur) {Pre=cur; intt = cur->val +carry; Cur->val = t%Ten; Carry= t/Ten; if(Carry = =0) Break; Cur= cur->Next; }        if(carry) Pre->next =NewListNode (1); returnreverse (Rev_head); } ListNode* Reverse (ListNode *head) {        if(!head)returnHead; ListNode*dummy =NewListNode (-1), *cur =Head; Dummy->next =Head;  while(cur->next) {ListNode*t = cur->Next; Cur->next = t->Next; T->next = dummy->Next; Dummy->next =T; }        returnDummy->Next; }};

We can also be implemented by recursion, so that we do not have to flip the list, through the recursive layer of the call, the first process is the chain tail element, we will add 1, and then see if there is carry, return carrying, and then back to the table header, add the carry, if the discovery and poor new carry, So let's add a new node at the beginning, see the code below:

Solution Two:

classSolution { Public: ListNode* PlusOne (listnode*head) {        if(!head)returnHead; intcarry =Helper (head); if(Carry = =1) {ListNode*res =NewListNode (1); Res->next =Head; returnRes; }        returnHead; }    intHelper (ListNode *node) {        if(!node)return 1; intCarry = Helper (node->next); intsum = Node->val +carry; Node->val = sum%Ten; returnSum/Ten; }};

The following method is more ingenious, the idea is to traverse the linked list, find the first number not 9, if the number is not found, indicating that all the numbers are 9, then the table header new value of 0 new node, add 1 processing, and then the right all the numbers are set to 0. For example:

For example 1->2->3, then the first is not 9 of the number is 3, 3 is added 1, into 4, the right no node, so do not handle, return to 1->2->4.

For example, say 8->9->9, find the first not 9 of the number is 8, 1 processing becomes 9, and then the back of the number is set 0, the result is 9->0->0.

Look at the situation of 9->9->9, can not find the number is not 9, then a new value of 0 in front of the node, the addition of 1 processing into 1, the back of the number is set 0, get 1->0->0->0.

Solution Three:

classSolution { Public: ListNode* PlusOne (listnode*head) {ListNode*cur = head, *right =NULL;  while(cur) {if(Cur->val! =9) Right =cur; Cur= cur->Next; }        if(!Right ) { Right=NewListNode (0); Right->next =Head; Head=Right ; }        ++right->Val; Cur= right->Next;  while(cur) {cur->val =0; Cur= cur->Next; }        returnHead; }};

Finally, this solution is the iterative method of two methods, we use the stack, using the advanced post-out mechanism, can be implemented from the forward processing node, see the code as follows:

Solution Four:

classSolution { Public: ListNode* PlusOne (listnode*head) {Stack<ListNode*>s; ListNode*cur =Head;  while(cur) {s.push (cur); Cur= cur->Next; }        intcarry =1;  while(!s.empty () &&carry) {ListNode*t =s.top (); S.pop (); intsum = T->val +carry; T->val = sum%Ten; Carry= SUM/Ten; }        if(Carry) {ListNode*new_head =NewListNode (1); New_head->next =Head; Head=New_head; }        returnHead; }};

Similar topics:

Plus One

Resources:

Https://leetcode.com/discuss/111165/2-accepted-java-solution

Https://leetcode.com/discuss/111205/simple-solution-use-recursion

Https://leetcode.com/discuss/111157/9-lines-recursive-without-helper

Https://leetcode.com/discuss/111155/java-stack-solution-with-inline-explanation

Leetcode all in one topic summary (continuous update ...)

[Leetcode] plus one Linked list linked list plus an operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.