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;
        }
    }
  • 相关阅读:
    npm --save-dev 与 --save 的区别
    Vue 简单实例 购物车2
    Vue 简单实例 购物车1
    node.js富文本编辑器
    使用jquery操作session
    浏览器窗口之间传递数据
    批量修改文件编码格式
    具有动态效果的响应式设计
    Ajax请求全局配置
    html实体转换
  • 原文地址:https://www.cnblogs.com/airwindow/p/4784360.html
Copyright © 2011-2022 走看看