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;
        }
    }
  • 相关阅读:
    APP-Android:APK
    软件-版本控制:VCS(版本控制系统)
    协议-网络-安全协议-SSH(安全外壳协议):百科
    un-解决方案-BIM:百科
    MySQL:常用语句
    rsync+inotify-tools文件实时同步
    Java实现 Leetcode 169 求众数
    Java实现 Leetcode 169 求众数
    Java实现 LeetCode 137 只出现一次的数字
    Java实现 LeetCode 137 只出现一次的数字
  • 原文地址:https://www.cnblogs.com/airwindow/p/4784360.html
Copyright © 2011-2022 走看看