forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT24.java
More file actions
70 lines (62 loc) · 1.94 KB
/
Copy pathT24.java
File metadata and controls
70 lines (62 loc) · 1.94 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package books;
/**
* @program JavaBooks
* @description: 反转链表
* @author: mf
* @create: 2019/09/05 09:55
*/
/*
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表
头节点。
*/
/*
思路:
设定三个指针,pre p next, 交换即可,但交换之前检查next是否为空,以防锻炼
*/
public class T24 {
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
ListNode listNode4 = new ListNode(4);
ListNode listNode5 = new ListNode(5);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
ListNode headNode = reverseListNode(listNode1);
System.out.println(headNode.value);
}
/**
* 循环
* @param headNode
* @return
*/
private static ListNode reverseListNode(ListNode headNode) {
if (headNode == null) return null;
ListNode pre = null; // 当前节点的前一个节点
ListNode cur = headNode; // 当前节点
while (cur != null) {
ListNode next = cur.next; // 存一下当前节点的下一个节点
cur.next = pre; // 将当前节点的下一个节点直接指向当前节点的pre
pre = cur; // 前一个节点指向当前节点
cur = next; // 当前节点指向下一个节点
}
return pre;
}
/**
* 尾递归
* @param headNode
* @return
*/
private static ListNode reverseListNode2(ListNode headNode) {
if (headNode == null) return null;
return reverse(null, headNode);
}
private static ListNode reverse(ListNode pre, ListNode cur) {
if (cur == null) return pre;
ListNode next = cur.next;
cur.next = pre;
return reverse(cur, next);
}
}