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;
        }
    }
  • 相关阅读:
    关于IP4上WIFI设置静态IP的一点经验
    迷你MVVM框架 avalonjs 1.1发布
    r.js合并实践
    IE6的checkbox, radio是通过defaultChecked决定是否选中
    2013年最后的收成:avalon1.0正式发布
    还要多少年, 前端开发才能像后端那样轻松
    JavaScript Promise:去而复返
    JavaScript 18岁生日快乐
    web界面上的字体兼容方案
    键盘钩子原理----开发按键发音程序
  • 原文地址:https://www.cnblogs.com/airwindow/p/4784360.html
Copyright © 2011-2022 走看看