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:

  • 相关阅读:
    Hadoop HDFS的Shell操作实例
    我来阿里的2年
    设计模式之迪米特原则(LoD)
    设计模式之接口隔离原则
    设计模式之依赖倒置原则
    设计模式之里氏替换原则(LSP)
    设计模式之单一职责原则(SRP)
    Android开发系列之性能优化
    Android开发系列之屏幕密度和单位转换
    Android开发系列之ListView
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4434861.html
Copyright © 2011-2022 走看看