-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution26.java
More file actions
49 lines (47 loc) · 1.13 KB
/
Solution26.java
File metadata and controls
49 lines (47 loc) · 1.13 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Swap Nodes in Pairs
* Given 1->2->3->4, you should return the list as 2->1->4->3
* should use only constant space
* You may not modify the values in the list, only nodes itself can be changed
*/
public class Solution26 {
static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode swapPairs(ListNode head) {
if (head != null && head.next != null) {
ListNode node = head;
ListNode result = head.next;
ListNode n = head.next.next;
result.next = node;
while (n != null && n.next != null) {
ListNode pre = n.next.next;
node.next = n.next;
node.next.next = n;
node = n;
n = pre;
}
node.next = n == null ? null : n;
return result;
}
return head;
}
public static void main(String ...args) {
Solution26 s26 = new Solution26();
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);
head = s26.swapPairs(head);
while (head != null) {
System.out.println(head.val);
head = head.next;
}
}
}