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.

  • 相关阅读:
    Windows 8的语音识别
    硬件驱动程序的知识点滴
    怎么将一张100KB以上大小的电子图片压缩成30KB以内
    Hadoop概念学习系列之Hadoop新手学习指导之入门需知(二十)
    Hadoop概念学习系列之Hadoop、Spark学习路线(很值得推荐)(十八)
    研究生,别再玩了,你玩不起!
    redis源代码解读之内存管理————zmalloc文件
    线段树之入门篇
    C#托付和事件
    dlmalloc 2.8.6 源代码具体解释(6)
  • 原文地址:https://www.cnblogs.com/freeneng/p/3086491.html
Copyright © 2011-2022 走看看