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;
        }
    }
  • 相关阅读:
    linux命令查询网站
    UTC(世界协调时间)时区和各个时区时间的转换
    dev-c++ 中写完源文件生成exe程序怎么避免运行闪退?
    numpy和time计时程序
    进化算法之粒子群算法和Matlab实现(多维)
    tf.config:GPU 的使用与分配(转载)
    限制TensorFlow只在CPU上运行的方法
    Python重要学习链接
    SpringCloud 之 Nacos 注册配置中心
    Jenkins 自动化部署入门详细教程
  • 原文地址:https://www.cnblogs.com/airwindow/p/4784360.html
Copyright © 2011-2022 走看看