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.

  • 相关阅读:
    使用自定义模板来弥补eclipse没有新建Filter的功能
    Eclipse快捷键-方便查找
    清除Eclipse中的内置浏览器中的历史记录
    Eclipse 调试器:零距离接触实战技巧
    Eclipse 插件开发 —— 深入理解查找(Search)功能及其扩展点
    JSEclipse—Eclipse上的JavaScript开发工具
    eclipse背景颜色调整参考(绿色养眼哟),其他工具也可以设置
    Eclipse,以及tomcat使用时可能会遇到的几个问题
    eclipse 快捷键设置
    eclipse3.3插件更新攻略
  • 原文地址:https://www.cnblogs.com/freeneng/p/3086491.html
Copyright © 2011-2022 走看看