How do you merge two sorted linked lists into one sorted list?
Simple meaning
Use a dummy head and append the smaller current node from either list, then attach any leftover tail.
Open the full page for Why, Steps, Example and Key takeaway.
Panel-ready DSA interview set questions for freshers and experienced developers. Practice at Coding Cadre in Faridabad, or Online from Delhi NCR.
Use a dummy head and append the smaller current node from either list, then attach any leftover tail.
Open the full page for Why, Steps, Example and Key takeaway.
Walk both lists with a carry, creating a new node for (sum % 10) and carrying sum // 10.
Open the full page for Why, Steps, Example and Key takeaway.
Find the middle with slow/fast, reverse the second half, compare pairwise, then optionally restore.
Open the full page for Why, Steps, Example and Key takeaway.
Two pointers walk both lists and switch to the other head on null
Open the full page for Why, Steps, Example and Key takeaway.
Split at the middle, reverse the second half, then weave nodes from the two halves.
Open the full page for Why, Steps, Example and Key takeaway.
The diameter is the max of left_height + right_height over every node (in edges).
Open the full page for Why, Steps, Example and Key takeaway.
Recurse: if the current node is p or q, return it
Open the full page for Why, Steps, Example and Key takeaway.
DFS subtracting the node value, succeeding when you hit a leaf and remaining target is zero.
Open the full page for Why, Steps, Example and Key takeaway.
A bottom-up DFS returns height or a sentinel if any subtree differs by more than one.
Open the full page for Why, Steps, Example and Key takeaway.
Level-order BFS and take the last node of each level, or DFS root-right-left and record the first node seen at each depth.
Open the full page for Why, Steps, Example and Key takeaway.
DFS with a legal (low, high) window: left child must be < node and stay > low, right must be > node and stay < high.
Open the full page for Why, Steps, Example and Key takeaway.
Inorder traversal visits keys in order
Open the full page for Why, Steps, Example and Key takeaway.
Walk from the root: go left if both keys are smaller, right if both are larger
Open the full page for Why, Steps, Example and Key takeaway.
Always pick the middle element as root and recurse on left and right halves so subtree sizes differ by at most one.
Open the full page for Why, Steps, Example and Key takeaway.
Count frequencies in a hash map, then push (freq, value) into a min-heap of size k.
Open the full page for Why, Steps, Example and Key takeaway.
Keep a max-heap of size k by squared distance so the farthest of the closest k sits at the top.
Open the full page for Why, Steps, Example and Key takeaway.
Count frequencies, reject if the max count exceeds (n+1)/2, then always place the current most frequent remaining character from a max-heap, holding the previous one out for a turn.
Open the full page for Why, Steps, Example and Key takeaway.
Push the first column (or first row) into a min-heap of (value, r, c) and pop k times, pushing the next item in that row each pop.
Open the full page for Why, Steps, Example and Key takeaway.
Scan cells
Open the full page for Why, Steps, Example and Key takeaway.
Hash original node to clone.
Open the full page for Why, Steps, Example and Key takeaway.