Leetcode 206. Reverse Linked List
方法1: 這道easy題兩個方法我都沒有解出來,主要原因是感覺還是接觸連結串列題目太少。第一個方法是iterate。這個方法關鍵在於要新建一個prev node,初始化為null。具體思路是curr node的next指向prev node,然後在向右移動curr node和pre node。時間複雜度n,空間複雜度1
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while(curr != null){
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
}
方法2: recursion。其實連結串列題的recursion邏輯並不是很難想,主要是正確的寫出這個邏輯,這中間會比較繞。時間複雜度n,空間複雜度n。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head ==null || head.next == null) return head;
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
}
總結:
- 這是一道非常經典的題目,雖然是easy題,但是學到了不少連結串列swap之類的操作
相關文章
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- 92. Reverse Linked List II
- 資料結構與演算法 | Leetcode 206:Reverse Linked List資料結構演算法LeetCode
- LeetCode | 141 linked list cycleLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- Leetcode 234. Palindrome Linked ListLeetCode
- LeetCode 382 Linked List Random NodeLeetCoderandom
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- Leetcode 142. Linked List Cycle IILeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- 1074 reverse list
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- 資料結構與演算法 | Leetcode 141:Linked List Cycle資料結構演算法LeetCode
- 328. Odd Even Linked List
- Leetcode 7 Reverse IntegerLeetCode
- 資料結構與演算法 | Leetcode 876. middle-of-the-linked-list資料結構演算法LeetCode
- LeetCode 83.Remove Duplicates from Sorted List(從已排序連結串列中除去重複) Easy/Linked ListLeetCodeREM排序
- [LeetCode Python 3] 876. Middle of the Linked List(連結串列的中間結點)LeetCodePython
- [LeetCode] 1367. Linked List in Binary Tree 二叉樹中的連結串列LeetCode二叉樹
- 114-Flatten Binary Tree to Linked List
- Leetcode 151 Reverse Words in a StringLeetCode
- leetcode 344. Reverse StringLeetCode
- LeetCode 之 JavaScript 解答第141題 —— 環形連結串列 I(Linked List Cycle I)LeetCodeJavaScript
- leetcode 206.反轉連結串列LeetCode
- leetcode 206. 反轉連結串列LeetCode
- Leetcode 25 Reverse Nodes in k-GroupLeetCode
- LeetCode Reverse Integer(007)解法總結LeetCode
- Leetcode 160. Intersection of Two Linked ListsLeetCode
- 【Leetcode】25.Reverse Nodes in k-GroupLeetCode
- LeetCode - 解題筆記 - 7 - Reverse IntegerLeetCode筆記
- [Leetcode力扣 25] Reverse Nodes in k-GroupLeetCode力扣
- 02-線性結構3 Reversing Linked List (25分)
- [LeetCode] 61. Rotate ListLeetCode