725. Split Linked List in Parts - Medium 前往題目 想法 沒想到如何處理前面需要補上的部分 思路 得到list總長度 利用除法以及取餘得到最少要多少個元素在一個part,然後需要補多少個 開始建造part,如果有需要補的也要加上去,一次補一個(題目規定) 建造完一個part就把尾巴斷開,補上null 很多條件需要考慮… Code class Solution { public ListNode[] splitLi 2024-10-14 Leetcode > Medium #Leetcode #心得 #Linked List
92. Reverse Linked List II - Medium 前往題目 想法 先找左右邊界,然後再根據左右邊界定位翻轉 思路結果left和right根本就是index,還是1-based的 找到left node 循環right - left + 1次,期間都翻轉node 最後再拼接 Code class Solution { public ListNode reverseBetween(ListNode head, int le 2024-10-11 Leetcode > Medium #Leetcode #心得 #Linked List
203. Remove Linked List Elements - Easy 前往題目 想法 直覺的操作,單純刪除node 思路 循環直到cur.next為null 如果next的值等於目標值那就把next賦值為next.next 否則移動當前指針到下一個 Codeclass Solution { public ListNode removeElements(ListNode head, int val) { ListNod 2024-10-10 Leetcode > Easy #Leetcode #心得 #Recursion #Linked List
2487. Remove Nodes From Linked List - Medium 前往題目 思路 反轉list 疊代所有元素,並紀錄最大值,小於最大值的話直接移除(略過) 再次反轉 Code class Solution { public ListNode removeNodes(ListNode head) { // Reverse the list head = reverse(head); 2024-10-08 Leetcode > Medium #Leetcode #心得 #Stack #Recursion #Monotonic Stack #Linked List
83. Remove Duplicates from Sorted List - Easy 前往題目 想法 簡單的去元素操作 思路想得有點太複雜,原本還想把去掉的元素的next也改成null,網友的解法並沒有這樣做,不知道gc會不會回收 循環直到cur.next為null 兩兩比較,如果數值一樣,就更改next為下一個,但不要移動當前的node,除非數值不同再移動 Code網友解答 class Solution { public ListNode deleteD 2024-10-08 Leetcode > Easy #Leetcode #心得 #Linked List
86. Partition List - Medium 前往題目 想法 循環兩次,一次組小於x的,一次組大於等於x的 思路其實一次就好,一起組 宣告兩個新的list 走訪每個ListNode,< x的放一組,>= x的放一組 兩組連起來 Codeclass Solution { public ListNode partition(ListNode head, int x) { ListNo 2024-10-07 Leetcode > Medium #Leetcode #心得 #Two Pointers #Linked List
1669. Merge In Between Linked Lists - Medium 前往題目 想法 簡單的拆開後連接 思路 找到第一個斷點,以及第二個斷點 把兩個斷點接上list2的頭和尾 Codeclass Solution { public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) { ListNode breakPointFro 2024-10-03 Leetcode > Medium #Leetcode #心得 #Linked List
2130. Maximum Twin Sum of a Linked List - Medium 前往題目 想法 儲存到arraylist後就可以一組一組看,不過會需要O(n)空間 思路 把list分一半 把後半的指針全部反轉 計算所有組的總和取最大 Code網友解答 class Solution { public int pairSum(ListNode head) { ListNode slow = head; ListNo 2024-10-02 Leetcode > Medium #Leetcode #心得 #Stack #Two Pointers #Linked List
707. Design Linked List - Medium 前往題目 思路這題就是實作linkedList,簡單但細節很多,很容易出錯 Codeclass MyLinkedList { private class Node { Node next; int val; public Node(int val) { this.next = null 2024-10-01 Leetcode > Medium #Leetcode #心得 #Linked List #Design
622. Design Circular Queue - Medium 前往題目 想法 自創node組成linkedlist 思路enQueue: 檢查是否已滿,未滿時可以加入新的node,如果是唯一一個,頭尾都是相同的node **deQueue**: 如果為空就false,否則彈出head`,並且大小減一 Code網友解答 class MyCircularQueue { int size; int maxSize; 2024-09-30 Leetcode > Medium #Leetcode #心得 #Queue #Array #Linked List #Design