Add numbers given in the form of Linked List
Two numbers are given in the form of linked list (with each node storing one digit). Write an algorithm which will compute the sum of these two linked lists. The result should be a linked list as below:
Find Merge-Point of two Linked Lists
Given two linked lists that merge at some point as shown below: Last four nodes are common to both the lists. Given pointers to the header nodes of each list, find the first common node.
Search in a sorted multi-level linked list
Given a multi-level linked list as shown in this post. Write code to search in such a list.
Printed a sorted multi-level linked list
Given a linked list, where each node has a pointer to the head of its child list (in addition to the next pointer). The child list is a normal linked list with each node having one data and one pointer to the next node in the child list. Let the lists be arranged (as above) […]
Traverse and print a linked list in forward and backward order
Given a linked list, write code to print the list in forward and backward order. Solution: This problem is discussed as an example in this post. A linked list is a data structure where address of next node is stored inside each node. Structure of Node Structure of Node of a list is as given […]
Rearrange the nodes of a linked list
Given a linked list, rearrange the node of the list as shown below: INPUT LIST: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 OUTPUT: 1 -> 8 -> 2 -> 7 -> 3 -> 6 -> 4 -> 5 INPUT LIST: 1 -> 2 -> 3 -> 4 […]
Merge alternate nodes of the two lists
We have already seen the code to merge two sorted linked list, such that the final list is also sorted. The logic and code can be found in this post. In this post, we want to merge the two lists in a way that in the final list, element comes from the two lists alternately […]
Convert a Binary Tree to a Doubly Linked List
The structure of Node of a Binary Tree and Doubly linked list are same. struct Node { int data; Node* left; Node* right; } Structure of Node of a Binary Tree struct Node { int data; Node* previous; Node* next; } Structure of Node of a Doubly linked list If we treat […]
Reversing a linked list iteratively using only 2 pointers
Given a linked list, write the non-recursive function to reverse the list. If Input list is 1 -> 2 -> 3 -> 4 -> 5 Then output should be 5 -> 4 -> 3 -> 2 -> 1 We have seen the recursive and non-recursive functions to reverse the list. In the non-recursive version, we take three […]
Recursive function to add 5 to alternate nodes of the linked list
Write a recursive function that add 5 to the alternate nodes of the linked list. For example, if the list is 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 then the function should change the list to the below one 1 -> 7 -> […]