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.

    链接: http://leetcode.com/problems/swap-nodes-in-pairs/

    题解:

    链表两两交换节点。需要建立一个dummy节点记录所交换两节点之前的节点,然后进行交换,dummy节点=head,head = head.next。画个图就很好明白了。

    Iterative:

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null)
                return head;
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode node = dummy;
            
            while(head != null && head.next != null){
                node.next = head.next;
                head.next = node.next.next;
                node.next.next = head;
                node = head;
                head = head.next;
            }
            
            return dummy.next;
        }
    }

    Recursive: 

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if(head == null || head.next == null)
                return head;
            
            ListNode temp = head.next.next;
            ListNode node = head;
            head = head.next;
            head.next = node;
            node.next = swapPairs(temp);
            
            return head;
        }
    }

    二刷:

    其实就是做一个local的操作就可以了,

    Java:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode node = dummy;
            while (head != null && head.next != null) {
                node.next = head.next;
                head.next = head.next.next;
                node.next.next = head;
                node = head;
                head = head.next;
            }
            return dummy.next;
        }
    }

    Python:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def swapPairs(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:
                return None
            dummy = ListNode(-1)
            dummy.next = head
            node = dummy
            while head and head.next:
                node.next = head.next
                head.next = head.next.next
                node.next.next = head
                node = head
                head = head.next
            return dummy.next

    三刷:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null) return head;
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode node = dummy;
            while (head != null && head.next != null) {
                node.next = head.next;
                head.next = head.next.next;
                node.next.next = head;
                node = head;
                head = head.next;
            }
            return dummy.next;
        }
    }

    Reference:

  • 相关阅读:
    进程间的通讯(IPC)方式
    进程间通信IPC之--共享内存
    TMDS协议
    HDMI接口与协议
    HDMI的CEC是如何控制外围互联设备的
    SVN并行开发管理策略
    关于 javascript event flow 的一个bug
    H面试程序(15): 冒泡排序法
    android应用如何启动另外一个apk应用
    [置顶] 一份诚恳的互联网找工作总结和感想(附:怎样花两年时间去面试一个人)
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4434861.html
Copyright © 2011-2022 走看看