206. Reverse Linked List - Easy
前往題目
想法
- 之前做過,忘了要用一個prev存上一個node
思路
- 疊代所有node
- 每個node都紀錄一下前一個,然後切換當前的node
Code
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode prev = null, curr = head;
while (curr != null) {
ListNode tempNode = curr.next;
curr.next = prev;
prev = curr;
curr = tempNode;
}
return prev;
}
}
206. Reverse Linked List - Easy
https://f88083.github.io/2024/01/28/206-Reverse-Linked-List-Easy/