-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
39 lines (35 loc) · 1.02 KB
/
Solution.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
37
38
39
package _024;
import structure.ListNode;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/01/31
* desc :
* </pre>
*/
public class Solution {
// public ListNode swapPairs(ListNode head) {
// if (head == null || head.next == null) return head;
// ListNode node = head.next;
// head.next = swapPairs(node.next);
// node.next = head;
// return node;
// }
public ListNode swapPairs(ListNode head) {
ListNode preHead = new ListNode(0), cur = preHead;
preHead.next = head;
while (cur.next != null && cur.next.next != null) {
ListNode temp = cur.next.next;
cur.next.next = temp.next;
temp.next = cur.next;
cur.next = temp;
cur = cur.next.next;
}
return preHead.next;
}
public static void main(String[] args) {
Solution solution = new Solution();
ListNode.print(solution.swapPairs(ListNode.createTestData("[1,2,3,4]")));
}
}