6-9 2023 review deck Flashcards
19: Remove Nth Node From End of List
For remove Nth node from the end of List, the 2 pointers allow us loop through the list once, fast and slow pointer, we need to set a dummy node before head, move fast Nth time, then move both fast and slow until we hit the end of the Linked list. Then slow should be at the Node before the Node that we need to remove
21: Merge Two Sorted Lists
Merge 2 sorted List - since we need to return a new list, we should create a dummy node, then build the list of the dummy node.
Compare the 2 list value, and add one at a time, the trick to remember is we might have list 1 or list 2 still have node after the while loop (while list1 and list2). So we need to account for the remainder node. We return dummy.next at the end
206: Reverse Linked List
Reverse Linked List - need to set next, prev, current pointer.
Next = current.next = prev = current = next, 4 line a circle
Return prev which the the new list beginning
121: Best Time to Buy and Sell Stock
Best time to buy and sell stock. We need to find the min price, and use today’s price - min price for the max profit, use min() to keep track of the min price and use max() to keep track of the Max profit
- Linked List Cycle
2 pointers, one fast, one slow, if slow == fast, meaning there is cycle in the linked list, the key is while fast and fast.next, if fast.next = None, then fast.next.next will error out.
- Linked List Cycle II
need to return the beginning of a linked-list cycle, we can use set to add the node into the set, if the first time node appear more than once, which mean that is the beginning of the cycle. we just need to loop through the linked-list, if the linked list reach to the end and no duplicate, then return None, there is no cycle
- Intersection of Two Linked Lists
Understand both set() and two pointers solution, 2 pointers is more optimal in term of using less space while set is easier to understand
86.Partition List
create 2 sublists, left list with all the lesser value, the right list with all the greater or equal value. connect the 2 lists and return at the end. one key point of the right list last node will point back to one of left list node which create a loop and error out
876.Middle of the Linked List
find the middle of the linked list, if there are 2 middle node, return the 2nd one, classic 2 pointers problem, fast and slow pointer, return slow pointer
23.Merge k Sorted Lists
extension of 21, merge 2 sorted list. Break the problem into 2 part, 1 helper function to merge 2 list, then try to reduce the lists in half each time. K lists, time logK * N
- Remove Duplicates from Sorted Array
fast slow pointer, when fast pointer reach the end, return slow + 1 for the total number of unique element
83.Remove Duplicates from Sorted List
same concept like 26, expect it is for linked-list, need to remember edge case and point the slow.next = None to avoid error
27.Remove Element
modify list in place, can modify the order, return the length of the array without the target value element, is very similar to 26, instead of remove the duplicates, now remove the target value, since we don’t need to worry about the order, we can start to swap the value as soon as we see any value difference from the target value
283.Move zeros
similar to remove duplicate 26, we need to move all the non-zero element to the front and preserve the relative order, keep track on the idx, and fill the idx to len(nums) with 0
- Reverse String
- given an array with char, return the reverse version of the array - 2 pointer, l, r , swap char in place, python can do s[l], s[r] = s[r], s[l]