zoukankan      html  css  js  c++  java
  • 24. Swap Nodes in Pairs

    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.

    经典解法:设一个哨兵然后前两个互换之后到后两个。

    public ListNode SwapPairs(ListNode head) {
             if (head == null) return head;
                var sentinel = new ListNode(-1);
                sentinel.next = head;
                var dummy = sentinel;
                while (head != null && head.next != null)
                {
                    dummy.next = head.next;
                    head.next = head.next.next;
                    dummy.next.next = head;
                    dummy = head;
                    head = head.next;
                }
      
                return sentinel.next;
            /*ListNode sentiential = new ListNode(0);
            sentiential.next = head;
            ListNode p = head;
            ListNode d = sentiential;
            while (p != null && p.next != null) {
            d.next = p.next;
            d = p;
            p = p.next.next;
            d.next.next = d;
        }
        d.next = p;
        return sentiential.next;*/
        }

    这个地方我一开始犯了一个错误是,即把注释掉的那行放在了下面.

    public ListNode SwapPairs(ListNode head) {
            if(head == null || head.next == null) return head;
            var sentinel = new ListNode(-1);
            sentinel.next = head;
            var dummy = sentinel;
            while(head != null && head.next != null) 
            {
                sentinel.next =  head.next;
                //head.next = head.next.next;
                sentinel.next.next = head;
                sentinel = head;
                head.next = head.next.next;
                head = head.next;
                
            }
            return dummy.next;
        }
        

    这个会使出现问题,head 为1。

    sentinel.next = head.next;

    => sentinel.next 为2

    sentinel.next.next = head;

    => 2.next = head = 1;
    此时1.next依然为2.  则1.next 为2,2.next 为1,deadlock.需要先将head.next 重新赋值.

    或者将list 间隔split成两个list,然后再逐个拼起来。

    public ListNode SwapPairs(ListNode head) {
            if(head == null || head.next == null) return head;
            var sec = head.next;
            var sentinel = new ListNode(-1);
            var dummy = sentinel;
            var headSentinel = head;
            ListNode nextNode = null;
            ListNode nextSNode =null;
            while(head != null && head.next != null)
            {
                sentinel.next = head.next;
                head.next = head.next.next;
                sentinel =  sentinel.next;
                head = head.next;
            }
            var front  = dummy.next;
            var second = headSentinel;
            while(front != null)
            {
                nextNode = front.next;
                nextSNode = second.next;
                front.next = second;
                second.next = nextNode;
                front = nextNode;
                second = nextSNode;
            }
           
            return dummy.next;
        }
        
  • 相关阅读:
    阿里云部署SSL证书详解
    PHP网站从Apache转移到Nginx后产生404错误的原因和解决办法
    APACHE服务器出现No input file specified.的完美解决方案
    Lnmp修改php.ini配置
    CGI,FASTCGI,PHPFPM的区别
    CLR Via CSharp读书笔记(25):线程基础
    Java Network Programming FAQ.
    SchemaFree MySQL vs NoSQL
    ERROR: JDWP Unable to get JNI 1.2 environment
    Firebug Internals
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5870273.html
Copyright © 2011-2022 走看看