Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes. Only nodes itself may be changed.
Solutions:
(1) Recursive: when head.next.next pairs are swapped, only need to swap the first two nodes
Java:
class Solution {
public ListNode swapPairs(ListNode head) {
if ((head == null) || (head.next == null))
return head;
ListNode q = swapPairs(head.next.next);
ListNode r = head.next;
r.next = head;
head.next = q;
return r;
}
}
// OR
class Solution {
public ListNode swapPairs(ListNode head) {
if ((head == null) || (head.next == null))
return head;
ListNode r = head.next;
head.next = swapPairs(head.next.next);
r.next = head;
return r;
}
}
Python:
class Solution(object):
def swapPairs(self, head):
if not head or not head.next: return head
new_start = head.next.next
head, head.next = head.next, head
head.next.next = self.swapPairs(new_start)
return head
// https://leetcode.com/problems/swap-nodes-in-pairs/discuss/11046/My-simple-JAVA-solution-for-share
(2) Iterative:
C++:
// Pointer-pointer pp points to the pointer to the current node. So at first, pp points to head, and later it points to the next field of ListNodes.
//Additionally, for convenience and clarity, pointers a and b point to the current node and the next node.
//We need to go from *pp == a -> b -> (b->next) to *pp == b -> a -> (b->next).
//The first three lines inside the loop do that, setting those three pointers (from right to left). The fourth line moves pp to the next pair.
ListNode* swapPairs(ListNode* head) {
ListNode **pp = &head, *a, *b;
while ((a = *pp) && (b = a->next)) {
a->next = b->next;
b->next = a;
*pp = b;
pp = &(a->next);
}
return head;
}
// https://leetcode.com/problems/swap-nodes-in-pairs/discuss/11019/7-8-lines-C%2B%2B-Python-Ruby
Python:
'''
Here, pre is the previous node. Since the head doesn't have a previous node, I just use self instead. Again, a is the current node and b is the next node.
To go from pre -> a -> b -> b.next to pre -> b -> a -> b.next, we need to change those three references. Instead of thinking about in what order I change them, I just change all three at once.
'''
def swapPairs(self, head):
pre, pre.next = self, head
while pre.next and pre.next.next:
a = pre.next
b = a.next
pre.next, b.next, a.next = b, a, b.next
pre = a
return self.next
# https://leetcode.com/problems/swap-nodes-in-pairs/discuss/11019/7-8-lines-C%2B%2B-Python-Ruby
# use 'dummydummy = pre = ListNode(0)' instead of 'self'
class Solution:
def swapPairs(self, head):
dummy = pre = ListNode(0)
pre.next = head
while pre.next and pre.next.next:
a = pre.next
b = a.next
pre.next, a.next, b.next = b, b.next, a
pre = a
return dummy.next
# by sfdye https://leetcode.com/problems/swap-nodes-in-pairs/discuss/11019/7-8-lines-C%2B%2B-Python-Ruby
Java:
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode current = dummy;
while (current.next != null && current.next.next != null) {
ListNode first = current.next;
ListNode second = current.next.next;
first.next = second.next;
current.next = second;
current.next.next = first;
current = current.next.next;
}
return dummy.next;
}
// https://leetcode.com/problems/swap-nodes-in-pairs/discuss/11046/My-simple-JAVA-solution-for-share