Leetcode Plus One Linked List

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/plus-one-linked-list/

Topic:

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

Exercises

Method 1:

The last plus one, if there is carry, it is necessary to change the Linked list before a Node, naturally think of reverse Linked list first. It's easier to do it from the beginning. Add up and reverse back.

Time Complexity:o (n). Reverse with O (n), reverse two times plus iterate once.

Space:o (1).

AC Java:

1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {val = x;}7  * }8  */9  Public classSolution {Ten      PublicListNode PlusOne (ListNode head) { One         if(Head = =NULL){ A             returnhead; -         } -ListNode tail =reverse (head); theListNode cur =tail; -ListNode pre =cur; -         intCarry = 1; -          +          while(cur! =NULL){ -Pre =cur; +             intsum = Cur.val +carry; ACur.val = sum%10; atcarry = SUM/10; -             if(Carry = = 0){ -                  Break; -             } -Cur =Cur.next; -         } in         if(Carry = = 1){ -Pre.next =NewListNode (1); to         } +         returnreverse (tail); -     } the      *     PrivateListNode Reverse (listnode head) { $         if(Head = =NULL|| Head.next = =NULL){Panax Notoginseng             returnhead; -         } theListNode tail =head; +ListNode cur =head; A ListNode pre; the ListNode temp; +          -          while(Tail.next! =NULL){ $Pre =cur; $Cur =Tail.next; -temp =Cur.next; -Cur.next =Pre; theTail.next =temp; -         }Wuyi         returncur; the     } -}

Method 2:

Recursion is used because the terminating condition of the recursion is to the end node, and each layer returns a carry value.

Time Complexity:o (n), up to a maximum of two times per node iterate.

Space:o (n), recursion uses the N-layer stack.

AC Java:

1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {val = x;}7  * }8  */9  Public classSolution {Ten      PublicListNode PlusOne (ListNode head) { One         if(Head = =NULL){ A             returnhead; -         } -         intcarry =Dfs (head); the         if(Carry = = 1){ -ListNode dummy =NewListNode (1); -Dummy.next =head; -             returndummy; +         } -         returnhead; +     } A      at     Private intDfs (ListNode head) { -         if(Head = =NULL){ -             return1; -         } -         intcarry =DFS (head.next); -         intsum = Head.val +carry; inHead.val = sum%10; -         returnSum/10; to     } +}

Method 3:

Same as Method 2, use stack instead of recursion.

Time Complexity:o (n).

Space:o (n). Stack size.

1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {val = x;}7  * }8  */9  Public classSolution {Ten      PublicListNode PlusOne (ListNode head) { One         if(Head = =NULL){ A             returnhead; -         } -stack<listnode> STK =NewStack<listnode>(); theListNode cur =head; -          while(cur! =NULL){ - Stk.push (cur); -Cur =Cur.next; +         } -         intCarry = 1; +          while(!stk.isempty () && carry = = 1){ AListNode top =Stk.pop (); at             intsum = Top.val +carry; -Top.val = sum%10; -carry = SUM/10; -         } -         if(Carry = = 1){ -ListNode dummy =NewListNode (1); inDummy.next =head; -             returndummy; to         } +         returnhead; -     } the}

Method 4:

From the right to the left to find the first node is not 9, found after the node plus a, if he has a node behind, indicating that the following nodes are 9, so all to become 0.

Time Complexity:o (n).

Space:o (1).

AC Java:

1 /**2 * Definition for singly-linked list.3 * public class ListNode {4 * int val;5 * ListNode Next;6 * ListNode (int x) {val = x;}7  * }8  */9  Public classSolution {Ten      PublicListNode PlusOne (ListNode head) { One         if(Head = =NULL){ A             returnhead; -         } -stack<listnode> STK =NewStack<listnode>(); theListNode cur =head; -          while(cur! =NULL){ - Stk.push (cur); -Cur =Cur.next; +         } -         intCarry = 1; +          while(!stk.isempty () && carry = = 1){ AListNode top =Stk.pop (); at             intsum = Top.val +carry; -Top.val = sum%10; -carry = SUM/10; -         } -         if(Carry = = 1){ -ListNode dummy =NewListNode (1); inDummy.next =head; -             returndummy; to         } +         returnhead; -     } the}

Leetcode Plus One Linked List

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.