• 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:

  • 相关阅读:
    阿里terway源码分析
    golang timeoutHandler解析及kubernetes中的变种
    第四章 控制和循环
    关于 自媒体的声明
    java用正则表达式获取url的域名
    javaweb三大核心基础技术
    NumPy之计算两个矩阵的成对平方欧氏距离
    C/C++之计算两个整型的平均值
    C/C++代码优化之整型除以2的指数并四舍五入
    SSE系列内置函数中的shuffle函数
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4434861.html
Copyright © 2020-2023  润新知