forked from JsonChao/Awesome-Algorithm-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution206.java
More file actions
42 lines (33 loc) · 966 Bytes
/
Copy pathSolution206.java
File metadata and controls
42 lines (33 loc) · 966 Bytes
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
package LinkedList_problem;
/**
* 1、pre、cur、next + 循环:
*
* 在遍历列表时,将当前节点的 next 指针改为指向前一个元素。
* 由于节点没有引用其上一个节点,因此必须事先存储其前一个元素。在更改引用之前,还
* 需要另一个指针来存储下一个节点。不要忘记在最后返回新的头引用!
*
* 时间复杂度:O(n)
* 空间复杂度:O(1)
*/
public class Solution206 {
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
// 1、pre、cur、next + 循环
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}