52. Linked-List: (LL medium-2)
Brute-
1. use a variable to store number represented by LL-1 (e.g., 3-->5 means 53)
2. use another variable to store number represented by LL-2
3. add these 2 variables and store in third variable
4. create a new LL using this third variable
---------------------------------------------
Optimal-
using concept of Dummy node
2. traverse both LL simultaneously using 2 pointers and add elements along with carry at each iteration
3. if sum<9 => good, else set new node to 0 with carry being 1
4. repeat till end
5. if carry remains at end, add 1 to the end of new LL
Brute- time: O(n) space: O(n)
Using hashing
1. traverse one LL and hash the nodes
2. Now, when u traverse the other LL, the first visited node you visit is poi
--------------------------------------------------
Better- time: O(n) space: O(1)
1. move pointer of Longer LL till it reaches starting posn of shorter LL
2. Then we start simultaneous traversal of both
---------------------------------------------------
Optimal-⭐
1. Move both ptrs starting from the respective heads of 2 LLs.
2. Whenever a ptr reaches last node, reset that ptr to head of other linked list & continue traversal of both pointers
3. Repeat
4. At a point, both ptrs will be vertically aligned, so then they would simultaneously reach common poi
Brute - time: O(nlogn) space: O(n)
1. store all elements in a vector
2. sort them
3. convert this array into a LL
-------------
Optimal- time: O(n) space: O(1)
using 2 pointers (merge step of merge sort algorithm)
use a dummy node to start new LL
---------------------------------------------------------------------------------------------------------------------------
Brute- time: O(NlogN) space: O(n), where n = total elements
1. store all elements in an array
2. sort array
3. convert array to a new LL
----------------------------------------------
Better- time: O(N*k) space: O(1)
repeatedly do merge 2 sorted LL in pairs of 2 over all k LL's
---------------------------------------------
Optimal- time: O(N*logK)⭐ space: O(k)
use min-heap
1. store all heads in a priority queue as {val, node}
2. create a dummy node as start point for final answer LL
3. Append min value node (out of pq) to dummy-->next
4. pop this node from pq & add its .next to pq at the same time
5. repeat
----------------------------------------------------------------------------------------------------------------------------
Brute- time: O(nlogn) space: O(n)
store all elements in a vector => sort them => over write the LL
------------------------------
Optimal- time: O(nlogn) space: O(1)
Merge Sort
2. left LL start from head & right LL start from (middle-->next)
3. separate the 2 LL (left LL is from head to mid, so update mid so it points to NULL) and call merge sort again, recursively for these 2 LL
4. merge these 2 sorted LL
- Tortoise hare algo returns 2nd middle in case of even length LL, but we want 1st middle in even length LL for split, so modify that algo
- so we will start fast from (head--->next) instead of head, in even length LL
Comments
Post a Comment