-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapNodesInParis.java
36 lines (34 loc) · 972 Bytes
/
SwapNodesInParis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
*
* @author chenqun
* 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.
*
*/
public class SwapNodesInParis {
public ListNode swapPairs(ListNode head){
ListNode ret = new ListNode(0);
ListNode p = ret;
while(head!=null && head.next!=null){
p.next = head.next;
head.next = head.next.next;
p.next.next = head;
p = p.next.next;
head = head.next;
}
if(head!=null)
p.next = head;
return ret.next;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
l1.next = l2;
l2.next = l3;
SwapNodesInParis test = new SwapNodesInParis();
System.out.println(test.swapPairs(l1).val);
}
}