zoukankan      html  css  js  c++  java
  • [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.

  • 相关阅读:
    cf492D Vanya and Computer Game
    cf492C Vanya and Exams
    cf492A Vanya and Cubes
    bzoj2038 [2009国家集训队]小Z的袜子(hose)
    bzoj3781 小B的询问
    bzoj1858 [Scoi2010]序列操作
    bzoj1060 [ZJOI2007]时态同步
    算法学习心得
    bzoj1054 [HAOI2008]移动玩具
    bzoj3437 小P的牧场
  • 原文地址:https://www.cnblogs.com/freeneng/p/3086491.html
Copyright © 2011-2022 走看看