[Leetcode 22] 24 Swap Nodes in Pairs

Problem:

Given a linked list, swap every two 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. You may not modify the values in the list, only nodes itself can be changed.

 

Analysis:

One simple brute force algorithm is to equally split the original linked list into 2 sub-lists, then merge them with the different order. See an example:

1 -> 2 -> 3 -> 4 -> 5 

Split into: 1->3->5 and 2->4.

Merger with the second list first: 2->1->4->3->5. That's  the result we want.

Another example with even number of nodes is as follows:

1->2->3->4->5->6

Split into: 1->3->5 and 2->4->6.

Then merge: 2->1->4->3->6->5. It's also correct;

Though it's correct, it will exceed the time limit due to the requiremnt of scan through the linkedlist 2 times with a time complexity O(2n).

 

Here we present a 3 pointers solution. Notice the invariant during the process. With 3 pointers, we make them point to 3 consecutive nodes in the list as follows.

n1 -> n2 -> n3

^       ^       ^

|     |   | 

P1     p2       p3

 

Step1: then update their linking relationships as follows: n2 points to n1, n1 points to n3. Then goto Step 2.

n2 -> n1 -> n3

^   ^    ^

|     |    |

p2     p1     p3

 

Step2: Now we need to move those 3 pointers to process remaining nodes: IF n3 isn's null and has a consecutive node, then p1 points to n3, p2 points to n3's next and p3 points to n3's next's next ELSE p1 points to p3 and break (we reach an end here). Then go to Step1.

 

Combine them into a consecutive procedure, it will looks like this.

1 -> 2  -> 3 -> 4 -> 5 -> 6 -> 7 -> 8

^     ^      ^

|   |   |

p1   p2    p3

 

2 -> 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8

^  ^  ^

|   |   |

p2  p1    p3

 

2 -> 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8

      ^  ^  ^

      |   |   |

      p1   p2     p3

 

2 -> 1 -> 4 -> 3 -> 5 -> 6 -> 7 -> 8

      ^  ^  ^

      |   |   |

      p1   p2     p3

 

2 -> 1 -> 4 -> 3 -> 5 -> 6 -> 7 -> 8

           ^  ^  ^

           |   |   |

           p1   p2     p3

 

ets...

 

Code:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode swapPairs(ListNode head) {
14         // Start typing your Java solution below
15         // DO NOT write main() function
16         if (head == null || head.next==null) return head;
17         
18         ListNode p1 = head, p2 = head.next, p3 = p2.next;
19         head = p2;
20         while (true) {
21             p2.next = p1;
22             
23             if ((p3 == null) || (p3.next == null)) {
24                 p1.next = p3;
25                 break;
26             } else {
27                 p1.next = p3.next;
28             }
29             
30             p1 = p3;
31             p2 = p3.next;
32             p3 = p2.next;
33         }
34         
35         return head;
36     }
37 }
View Code

 

Attention:

Analyze and find the invariant is important here.

posted on 2013-05-19 08:46  freeneng  阅读(159)  评论(0编辑  收藏  举报

导航