- 浮华生
2019-05-30
·
via 浮华生

Consideration
- make three pointers , pre,current,next;
- initial pre as null
- use tmp to save current’s next node info
- change current’s next to link pre node(first is null)
- move pre pointer to next node
- move current pointer to next node
soultion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode current = head;
while (current != null){
ListNode next = current.next;
current.next = pre;
pre = current;
current = next;
}
return pre;
}
}
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。