Linked List: Delete nodes which have a greater value on right side using recursion
Problem Statement: You are given a LL, you need to remove all the nodes that have a greater value to their right side Example Input: 11 -> 14 -> 9 -> 10 -> 4 -> 5 -> 1 -> 2 -> NULL Output: 14 -> 10 -> 5 -> 2 -> NULL Solution In this […]
Linked List: Multiply two numbers represented by Linked Lists
Problem Statement: You are given 2 LL, you need to get the multiplication of the 2 LL. Example Input: 1 -> 2 2 Output: 24 Solution We follow bellow steps for the solution: 1. Initialize a variable 2. Start traversing the LL 3. Add the first value to the variable. 4. From the next node, […]
Linked List: Flatten a Linked List
Problem Statement: You are given a LL that has below properties: 1. Pointer to the next node in the main list 2. Pointer to a sorted LL with the node is head Example Input: 4 -> 9 -> 18 -> 27 | | | | V V V V 6 18 21 32 | | […]
Linked List: Rotate a Doubly Linked list in group of Given Size
Problem Statement: You are given DLL and a number ‘k’. You need to reverse every group of k nodes in the list Example Input: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 k = 2 Output: 2 <-> 1 <-> 4 <-> 3 <-> 6 <-> 5 Solution We create a recursive […]
Linked List: Rotate Doubly linked list by N nodes
Problem Statement: You are given a DLL and a integer N. You need to rotate the DLL counter clock wise Example Input : a b c d e N = 2 Output : c d e a b Solution The solution is very easy. We need 3 pointer to hold 3 nodes: Nth node, (N+1) […]
Linked List: Sort a “k”sorted Doubly Linked list
Problem Statement: You are given a DLL and each node is at most ‘k’ away from its target position. You need to sort the DLL Example Example: 3 <-> 6 <-> 2 <-> 12 <-> 56 <-> 8 k = 2 Output: 2 <-> 3 <-> 6 <-> 8 <-> 12 <-> 56 Solution You […]
Linked List: Count triplets in a sorted DLL whose sum is equal to given value “k”.
Problem Statement: You are given a Sorted DLL and a value ‘k’. You need to get the triplets that the sum is equal to k Example Example: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 k = 6 Output: 1 Solution Method 1: Use 3 loops and generate all triplets and check […]
Linked List: Find pairs with a given sum in a DLL.
Problem Statement: You are given a DLL and a value, get the pairs that match the value. Example Example: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 k = 5 Output: ( 2, 3) Solution Method 1: Two pointers Approach Take 2 pointers “first = head” and “second=last_node” If the current sum […]
Linked List: Reverse a Doubly Linked List
Problem Statement: You are given an DLL you need to reverse the given DLL. Example Input: -> 1 -> 2 -> 3 -> 4 <- <- <- Output: -> 4 -> 3 -> 2 -> 1 <- <- <- Solution The solution is very simple. We need to swap prev and next pointers for all […]
Linked List: Deletion from a Circular Linked List
Problem Statement: You are given a circular linked list, and a node value. You need to delete that node from the list. Example Input: -> 1 -> 2 -> 3 -> 4 ^ | | _ _ _ _ _| Node Data = 2 Output: -> 1 – -> 3 -> 4 ^ | | […]