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:

posted @ 2015-04-17 14:26  YRB  阅读(682)  评论(0编辑  收藏  举报