forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapPairs_24.java
More file actions
55 lines (53 loc) · 1.44 KB
/
Copy pathSwapPairs_24.java
File metadata and controls
55 lines (53 loc) · 1.44 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
50
51
52
53
54
55
public class SwapPairs_24 {
class ListNode {
int val;
ListNode next;
ListNode(){};
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
/**
* 递归解法:
* 1、定义两个指针head和next分别指向前两个元素,对后面的元素递归
* 2、交换head和next两个元素和递归返回的元素相连
* @param head
* @return
*/
public ListNode swapPairs1(ListNode head) {
if(head == null || head.next == null) return head;
ListNode nextNode = head.next;
head.next = swapPairs1(nextNode.next);
nextNode.next = head;
return nextNode;
}
/**
* 迭代法求解:需要定义三个指针
* @param head
* @return
*/
public ListNode swapPairs2(ListNode head) {
if(head == null) {
return null;
}
ListNode next = head.next;
ListNode pre = new ListNode();
ListNode preNode = pre;
pre.next = head;
preNode.next = head;
while(head != null && next != null) {
ListNode temp = next.next;
next.next = head;
pre.next = next;
head.next = temp;
pre = head;
head = temp;
if(head != null) next = head.next;
}
return preNode.next;
}
}