發表文章

目前顯示的是有「LinkedList」標籤的文章

Leetcode 83. Remove Duplicates from Sorted List

圖片
自己想到遞迴的解法,但效能不是說很好。 讓head->next = func(head->next),如果下一個和當前一樣就return func(head->next),

Leetcode 206. Reverse Linked List

圖片
首先先用兩個指針來維護此Linked List,一個叫做pre、一個叫做cur。 而temp就是每次處理的對象,要將往前指,所以temp是cur->next。 cur做的是每一次的遍歷,遍歷目標在temp下一個。 temp下一個就是尾巴,尾巴就是pre->next。 pre做的是就是複雜尾巴的部分,每次結束把temp給他。而pre->next就是temp。 另外一個方法是:一開始用TempList儲存處理的對象(一開始尾巴是NULL),遍歷head。 尾巴等於tail,TempList assign給tail。TempList 等於頭,頭等於頭->next。TempList下一個等於tail。一直循環直到head == NULL

Leetcode 141. Linked List Cycle

圖片

Leetcode 876. Middle of the Linked List

圖片
首先觀察奇數與偶數的關係,上面範例為長度5的linked list,會取index 2。長度6的linked list會取index 3。所以即 ans = arr [n / 2] 求Linked list中間的數字,我的方法為:

Leetcode 203. Remove Linked List Elements

圖片
遞迴方法: 思考方式Top-down,大問題可以拆解小問題 頭指針的下一個是 1. 如果==val 則會return ->next->next 2. 否則 return ->next 缺點容易stack overflow;