24 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
Note:
- Your algorithm should use only constant extra space.
- You may not modify the values in the list's nodes, only nodes itself may be changed.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
// 1 -> 2 -> 3 -> 4 -> null
// 5 <-4 <- 3<- 1 <- 2
// l2 l1
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode l1 = dummy;
ListNode l2 = head;
while(l2 != null && l2.next != null) {
ListNode nextHead = l2.next.next;//points to 3
l1.next = l2.next;
l2.next.next = l2;
l2.next = nextHead;
l1 = l2;
l2 = l2.next;
}
return dummy.next;
}
}