Leetcode:swap Nodes in Pairs problem solving report

Source: Internet
Author: User

Swap Nodes in Pairs

Given a linked list, swap every, adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. Modify the values in the list, only nodes itself can be changed.

Solution 1:

Recursive solution:

1. Turn back the linked list first to get the new next.

2. Flip the current 2 nodes.

3. Return to the new header.

1 //Solution 1:the recursion version.2      PublicListNode swapPairs1 (ListNode head) {3         if(Head = =NULL) {4             return NULL;5         }6         7         returnRec (head);8     }9     Ten      PublicListNode Rec (listnode head) { One         if(Head = =NULL|| Head.next = =NULL) { A             returnhead; -         } -          theListNode next =Head.next.next; -          -         //Flip back the linked list -Next =Rec (next); +          -         //Store the new head. +ListNode tmp =Head.next; A          at         //reverse the nodes. -Head.next =Next; -Tmp.next =head; -          -         returntmp; -}
View Code

Solution 2:

Iterative solution:

1. Using dummy node to save the previous node of the head node

2. Record the pre (previous node) of the rollover area and record the next, or tail, of the rollover area.

3. Flip a specific area and keep moving forward.

There are 2 ways to write, and the latter one is slightly simpler, and the next node in the flipped area is recorded.

1 //Solution 2:the Iteration version.2      PublicListNode Swappairs (ListNode head) {3         //if less than 2 elements, no action is required4         if(Head = =NULL|| Head.next = =NULL) {5             returnhead;6         }7         8ListNode dummy =NewListNode (0);9Dummy.next =head;Ten          One         //The node before the reverse area; AListNode pre =dummy; -          -          while(Pre.next! =NULL&& Pre.next.next! =NULL) { the             //The last node of the reverse area; -ListNode tail =Pre.next.next; -              -ListNode tmp =Pre.next; +Pre.next =tail; -              +ListNode next =Tail.next; ATail.next =tmp; atTmp.next =Next; -              -             //move forward the pre node. -Pre =tmp; -         } -          in         returnDummy.next; -}
View Code

1 //Solution 3:the Iteration version.2      PublicListNode SwapPairs3 (ListNode head) {3         //if less than 2 elements, no action is required4         if(Head = =NULL|| Head.next = =NULL) {5             returnhead;6         }7         8ListNode dummy =NewListNode (0);9Dummy.next =head;Ten          One         //The node before the reverse area; AListNode pre =dummy; -          -          while(Pre.next! =NULL&& Pre.next.next! =NULL) { theListNode next =Pre.next.next.next; -              -ListNode tmp =Pre.next; -Pre.next =Pre.next.next; +Pre.next.next =tmp; -              +Tmp.next =Next; A                          at             //move forward the pre node. -Pre =tmp; -         } -          -         returnDummy.next; -}
View Code

GITHUB:

Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/SwapPairs3.java

Leetcode:swap Nodes in Pairs problem solving report

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.