0% found this document useful (0 votes)
199 views7 pages

SET - 9 (Link List) PDF

The document describes 23 problems related to linked lists that should be solved iteratively and recursively. The problems include pairwise swapping elements, deleting alternate nodes, alternating splits, sorting using merge and quick sort, reversing groups of nodes, reversing alternate nodes, deleting nodes with greater values on the right side, segregating even and odd nodes, detecting and removing loops, adding two linked lists, finding triplets with a given sum, rotating lists, sorting 0s, 1s and 2s, swapping kth from beginning with kth from end, deleting nodes after m nodes, merging lists at alternate positions, reversing and appending alternate nodes, sorting lists with alternating ascending and descending orders, swapping nodes without swapping data, flattening mult

Uploaded by

MUKUL MANDLOI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
199 views7 pages

SET - 9 (Link List) PDF

The document describes 23 problems related to linked lists that should be solved iteratively and recursively. The problems include pairwise swapping elements, deleting alternate nodes, alternating splits, sorting using merge and quick sort, reversing groups of nodes, reversing alternate nodes, deleting nodes with greater values on the right side, segregating even and odd nodes, detecting and removing loops, adding two linked lists, finding triplets with a given sum, rotating lists, sorting 0s, 1s and 2s, swapping kth from beginning with kth from end, deleting nodes after m nodes, merging lists at alternate positions, reversing and appending alternate nodes, sorting lists with alternating ascending and descending orders, swapping nodes without swapping data, flattening mult

Uploaded by

MUKUL MANDLOI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SET – 9

[ Link list (Set – 2)]


** All the Program should be done by Iterative and Recursively both.
1. Pairwise swap elements of a given linked list
Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list
is 1->2->3->4->5 then the function should change it to 2->1->4->3->5, and if the linked list is 1->2-
>3->4->5->6 then the function should change it to 2->1->4->3->6->5.

2. Delete alternate nodes of a Linked List


Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For
example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5,
and if the given linked list is 1->2->3->4 then convert it to 1->3.

3. Alternating split of a given Singly Linked List


Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller
lists ‘a’ and ‘b’. The sublists should be made from alternating elements in the original list. So if the
original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and the other should be
1->1->1.

4. Sort Linked Lists and Doubly link list using Merge Sort and Quick
Sort.

5. Reverse a Linked List in groups of given size


Given a linked list, write a function to reverse every k nodes (where k is an input to the function).

Example:

Inputs: 1->2->3->4->5->6->7->8->NULL and k = 3

Output: 3->2->1->6->5->4->8->7->NULL.

Inputs: 1->2->3->4->5->6->7->8->NULL and k = 5

Output: 5->4->3->2->1->8->7->6->NULL.

6. Reverse alternate K nodes in a Singly Linked List


Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the
function) in an efficient way. Give the complexity of your algorithm.
Example:
Inputs: 1->2->3->4->5->6->7->8->9->NULL
and k = 3
Output: 3->2->1->4->5->6->9->8->7->NULL.
7. Delete nodes which have a greater value on right side
Given a singly linked list, remove all the nodes which have a greater value on right side.
Examples:
a) The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. Note
that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side.
When we examine 12, we see that after 12 there is one node with value greater than 12 (i.e. 15),
so we delete 12.
When we examine 15, we find no node after 15 that has value greater than 15 so we keep this
node.
When we go like this, we get 15->6->3
b) The list 10->20->30->40->50->60->NULL should be changed to 60->NULL. Note that 10, 20, 30,
40 and 50 have been deleted because they all have a greater value on the right side.
c) The list 60->50->40->30->20->10->NULL should not be changed.

8. Segregate even and odd nodes in a Linked List


Given a Linked List of integers, write a function to modify the linked list such that all even numbers
appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd
numbers same.
Examples:
Input: 17->15->8->12->10->5->4->1->7->6->NULL
Output: 8->12->10->4->6->17->15->5->1->7->NULL
Input: 8->12->10->5->4->1->6->NULL
Output: 8->12->10->4->6->5->1->NULL
If all numbers are even then do not change the list
Input: 8->12->10->NULL
Output: 8->12->10->NULL
If all numbers are odd then do not change the list
Input: 1->3->5->7->NULL
Output: 1->3->5->7->NULL

9. Detect and Remove Loop in a Linked List


Write a function detectAndRemoveLoop() that checks whether a given Linked List contains loop
and if loop is present then removes the loop and returns true. And if the list doesn’t contain loop
then returns false. Below diagram shows a linked list with a loop. detectAndRemoveLoop() must
change the below list to 1->2->3->4->5->NULL.
10.Add two numbers represented by linked lists
Given two numbers represented by two lists, write a function that returns sum list. The sum list i s
list representation of addition of two input numbers.
Example 1

Input:

First List: 5->6->3 // represents number 365

Second List: 8->4->2 // represents number 248

Output

Resultant list: 3->1->6 // represents number 613

Example 2

Input:

First List: 7->5->9->4->6 // represents number 64957

Second List: 8->4 // represents number 48

Output

Resultant list: 5->0->0->5->6 // represents number 65005

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″.

12.Rotate a Linked List


Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given
positive integer.For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list
should be modified to 50->60->10->20->30->40. Assume that k is smaller than the count of nodes
in linked list.

13. Sort a linked list of 0s, 1s and 2s


Given a linked list of 0s, 1s and 2s, sort it.

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

Linked List: 1->2->3->4->5->6->7->8

Output:

Linked List: 1->2->5->6

Input:

M = 3, N = 2

Linked List: 1->2->3->4->5->6->7->8->9->10

Output:

Linked List: 1->2->3->6->7->8

Input:

M = 1, N = 1

Linked List: 1->2->3->4->5->6->7->8->9->10

Output:

Linked List: 1->3->5->7->9

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

Input List: 1->2->3->4->5->6

Output List: 1->3->5->6->4->2

Input List: 12->14->16->18->20

Output List: 12->16->20->18->14

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

Input List: 10->40->53->30->67->12->89->NULL


Output List: 10->12->30->43->53->67->89->NULL

19. Swap nodes in a linked list without swapping data


Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by
changing links. Swapping data of nodes may be expensive in many situations when data contains
many fields.
It may be assumed that all keys in linked list are distinct.
Examples:

Input: 10->15->12->13->20->14, x = 12, y = 20

Output: 10->15->20->13->12->14

Input: 10->15->12->13->20->14, x = 10, y = 20

Output: 20->15->12->13->10->14

Input: 10->15->12->13->20->14, x = 12, y = 13

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?

20. Flatten a multilevel linked list


Given a linked list where in addition to the next pointer, each node has a child pointer, which may
or may not point to a separate list. These child lists may have one or more children of their own,
and so on, to produce a multilevel data structure, as shown in below figure.You are given the head
of the first level of the list. Flatten the list so that all the nodes appear in a single-level linked list.
You need to flatten the list in way that all nodes at first level should come first, then nodes of second
level, and so on.

The above list should be converted to 10->5->12->7->11->4->20->13->17->6->2->16->9->8->3->19->15

22. Select a Random Node from a Singly Linked List


Given a singly linked list, select a random node from linked list (the probability of picking a node should be
1/N if there are N nodes in list). You are given a random number generator.

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

we switch at 3 and 240 to get above maximum sum linked list

24. Generic Linked List in C


Unlike C++ and Java, C doesn’t support generics. How to create a linked list in C that can be used for any
data type? In C, we can use void pointer and function pointer to implement the same functionality. The
great thing about void pointer is it can be used to point to any data type. Also, size of all types of pointers
is always is same, so we can always allocate a linked list node. Function pointer is needed process actual
content stored at address pointed by void pointer.

25. Clone a linked list with next and random pointer


You are given a Double Link List with one pointer of each node pointing to the next node just like in a single
link list. The second pointer however CAN point to any node in the list and not just the previous node. Now
write a program in O(n) time to duplicate this list. That is, write a program which will create a copy of this
list.
Let us call the second pointer as arbit pointer as it can point to any arbitrary node in the linked list.

Arbitrary pointers are shown in red and next pointers in black

!!! All the Best!!!

You might also like