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.
Example:
Given1->2->3->4
, you should return the list as2->1->4->3.
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class num24 {
public ListNode swapPairs(ListNode head) {
ListNode result = new ListNode(0);
result.next = head;
ListNode current = result;
while(current.next!=null && current.next.next!=null){
ListNode first = current.next;
ListNode second = current.next.next;
first.next = second.next;
current.next = second;
second.next = first;
current = current.next.next;
}
return result.next;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
num24 s = new num24();
ListNode result = s.swapPairs(head);
while(result!=null){
System.out.println(result.val);
result = result.next;
}
}
}