SET - 9 (Link List) PDF
SET - 9 (Link List) PDF
4. Sort Linked Lists and Doubly link list using Merge Sort and Quick
Sort.
Example:
Output: 3->2->1->6->5->4->8->7->NULL.
Output: 5->4->3->2->1->8->7->6->NULL.
Input:
Output
Example 2
Input:
Output
11.Find a triplet from three linked lists with sum equal to a given number
Given three linked lists, say a, b and c, find one node from each list such that the sum of the
values of the nodes is equal to a given number.
For example, if the three linked lists are 12->6->29, 23->5->8 and 90->20->59, and the given
number is 101, the output should be triplet “6 5 90″.
14. Swap Kth node from beginning with Kth node from end in a Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data
is not allowed, only pointers should be changed. This requirement may be logical in many
situations where the linked list data part is huge (For example student details line Name, RollNo,
Address,…etc.). The pointers are always fixed (4 bytes for most of the compilers).
15. Delete N nodes after M nodes of a linked list
Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes
then delete next N nodes, continue the same till end of the linked list.
Examples:
Input:
M = 2, N = 2
Output:
Input:
M = 3, N = 2
Output:
Input:
M = 1, N = 1
Output:
16. Merge a linked list into another linked list at alternate positions
Given two linked lists, insert nodes of second list into first list at alternate positions of first list.
For example, if first list is 5->7->17->13->11 and second is 12->10->2->4->6, the first list should become
5->12->7->10->17->2->13->4->11->6 and second list should become empty. The nodes of second list
should only be inserted when there are positions available. For example, if the first list is 1->2->3 and
second list is 4->5->6->7->8, then first list should become 1->4->2->5->3->6 and second list to 7->8.
Use of extra space is not allowed (Not allowed to create additional nodes), i.e., insertion must be done in-
place.
17. Given a linked list, reverse alternate nodes and append at the end
Given a linked list, reverse alternate nodes and append them to end of list. Extra allowed space is O(1)
Examples
18. Sort a linked list that is sorted alternating ascending and descending
orders?
Given a Linked List. The Linked List is in alternating ascending and descending orders. Sort the list
efficiently.
Example
Output: 10->15->20->13->12->14
Output: 20->15->12->13->10->14
Output: 10->15->13->12->20->14
This may look a simple problem, but is interesting question as it has following cases to be
handled.
1) x and y may or may not be adjacent.
2) Either x or y may be a head node.
3) Either x or y may be last node.
4) x and/or y may not be present in linked list.
How to write a clean working code that handles all of the above possibilities?
23. Construct a Maximum Sum Linked List out of two Sorted Linked Lists
having some Common nodes
Given two sorted linked lists, construct a linked list that contains maximum sum path from start to end. The
result list may contain nodes from both input lists. When constructing the result list, we may switch to the
other input list only at the point of intersection (which mean the two node with the same value in the lists).
You are allowed to use O(1) extra space.
Input:
List1 = 1->3->30->90->120->240->511
List2 = 0->3->12->32->90->125->240->249
Output: Following is maximum sum linked list out of two input lists
list = 1->3->12->32->90->125->240->511