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

    This problem is easy! But take care of updating pointer, otherwise a infinite loop case may appear.
    Basic idea:
    If there are more than nodes left in the LinkedList, use node1 to point the first node and use node2 to point the second node, then make exchange of those two nodes, and continue ...

    Wrong Solution:

    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null)
                return head;
            ListNode dummy = new ListNode(1);
            ListNode pre = dummy;
            dummy.next = head;
            ListNode node1 = head;
            ListNode node2 = head.next;
            while (node1 != null && node2 != null) {
                ListNode temp = node2.next;
                pre.next = node2;
                node2.next = node1;
                node1.next = temp;if (node2.next != null) {
                    node1 = node2.next;
                    if (node2.next.next != null)
                        node2 = node2.next.next;
                }
            }
            return dummy.next;
        }
    }

    Mistakes:

    Error case
    Last executed input:
    [1,2]
    
    Mistake 1:
    mistake 1: forget to update the pointer of node1 and node2.
    temp = node1;
    node1 = node2;
    node2 = temp;
    pre = node2;
    
    mistake 2: if there are not enough nodes(2 nodes) left, you should also update on node2, to indicate the loop should over!
    if (node2.next != null && node2.next.next != null) {
        node1 = node2.next;
        node2 = node2.next.next;
    } else{
        //inorder to exit the loop, no effect on node in the linkedlist
        node2 = null;
    }

    Solution:

    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null)
                return head;
            ListNode dummy = new ListNode(1);
            ListNode pre = dummy;
            dummy.next = head;
            ListNode node1 = head;
            ListNode node2 = head.next;
            while (node2 != null) {
                ListNode temp = node2.next;
                pre.next = node2;
                node2.next = node1;
                node1.next = temp;
                
                temp = node1;
                node1 = node2;
                node2 = temp;
                pre = node2;
                
                if (node2.next != null && node2.next.next != null) {
                    node1 = node2.next;
                    node2 = node2.next.next;
                } else{
                    //inorder to exit the loop, no effect on node in the linkedlist
                    node2 = null;
                }
            }
            return dummy.next;
        }
    }
  • 相关阅读:
    qt中使用dll库的方法
    41. wait notify 方法
    40.方式二的练习
    39.线程的实现方式二
    38. 死锁
    37. 解决线程问题方式二(同步函数)
    36. 解决线程问题方式一(同步代码块synchronized)
    35.线程问题
    34. Thread类的常用方法
    33. 自定义线程
  • 原文地址:https://www.cnblogs.com/airwindow/p/4784360.html
Copyright © 2011-2022 走看看