Linked - List Assignment 2
Linked - List Assignment 2
Linked - List Assignment 2
2. Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1) and l2 = (4,
5), after return from concatenate (l1, l2) the list l1 should be changed to be l1 = (2, 3,
1, 4, 5). Your function should not change l2 and should not directly link nodes from l1
to l2 (i.e. the nodes inserted into l1 should be copies of the nodes from l2.)
3. Write a function to reverse the nodes in a linked list. Your function should have time
complexity O(n), where n is the length of the list. You should create no new nodes.
4. Print all the elements at the index of multiples of k with the first element assumed to
have an index of 0. Do this for a single pass of the linked list.
Input:
k=3
12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output:
12 -> 17 -> 22 -> NULL
5. Extend the above solution assuming that the list is circular and the N-th index is the
same as 0-th index. You may need multiple passes. However, every number should be
printed only once during its first selection.
Input:
k=3
12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output:
12 -> 17 -> 22 -> 18 -> 20 -> 15 -> 19 -> NULL
6. Delete duplicate elements from a given linked list. Retain the earliest entries.
Input:
20 -> 18 -> 15 -> 20 -> 6 -> 18 -> 5 -> 3 -> NULL
Output:
20 -> 18 -> 15 -> 6 -> 5 -> 3 -> NULL
8. Remove in a linked list all the nodes that have a greater value to their right.
Input:
10 -> 12 -> 15 -> 20 -> 5 -> 16 -> 25 -> 8 -> NULL
10 -> 12 -> 15 -> 20 -> 25 -> 26 -> 30 -> 40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL
Output:
20 -> 25 -> 8 -> NULL
40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL
11. Compare the worst-case big-O time analysis for these two functions: The insertfunction
for the sequence that is implemented using a fixed-sized array, and the insert function
for the sequence that is implemented using a linked list.
12. Find the middle element of a given linked list. In case of tie print the second one.
Input:
5 -> 7 -> NULL
5 -> 7 -> 17 -> NULL
5 -> 7 -> 17 -> 13 -> NULL
5 -> 7 -> 17 -> 13 -> 11 -> NULL
Output:
7
7
17
17
14. Calculate the frequency of occurrence of each element in a given linked list in the
same order they appear. Avoid printing multiple entries.
Input:
20 -> 18 -> 15 -> 20 -> 6 -> 18 -> 5 -> 18 -> NULL
Output:
Freq(20) = 2
Freq(18) = 3
Freq(15) = 1
Freq(6) = 1
Freq(5) = 1
15. Given a pair linked lists, insert nodes of second linked list into the first linked list at
alternate positions. Assume that the first linked list has at least as many elements as the
second.
Input:
1 -> 2 -> 3 -> NULL
4 -> 5 -> NULL
Output:
1 -> 4-> 2 -> 5 -> 3 -> NULL
16. Write a C program to find a pair in a singly linked list whose sum is equal to a given
value.
Test Data and Expected Output:
Original singly linked list:
1234567
Find a pair whose sum is equal to 4:
(1,3)
Find a pair whose sum is equal to 11:
(4,7) (5,6)
Find a pair whose sum is equal to 5:
(1,4) (2,3)
Find a pair whose sum is equal to 14:
Pair not found.
17. Write a C program to reverse alternate k nodes of a given singly linked list.
Test Data and Expected Output :
Original List: 1 2 3 4 5 6 7 8
Reverse alternate k (k=2) nodes of the said singly linked list:
21346578
Reverse alternate k (k=3) nodes of the said singly linked list:
31246587
Reverse alternate k (k=4) nodes of the said singly linked list:
42136587
18. Write a program in C to insert a new node in the middle of a doubly linked list.
Test Data and Expected Output :
Doubly Linked List : Insert new node at the middle in a doubly linked list :
Input the number of nodes (3 or more): 3
Input data for node 1 : 2
Input data for node 2 : 4
Input data for node 3 : 5
Data entered in the list are :
node 1 : 2
node 2 : 4
node 3 : 5
Input the position ( 2 to 2 ) to insert a new node : 2
Input data for the position 2 : 3
After insertion the new list are :
node 1 : 2
node 2 : 3
node 3 : 4
node 4 : 5
19. Write a program in C to delete a node from the last node of a doubly linked list.
Test Data and Expected Output :
Input the number of nodes (3 or more ): 3
Input data for node 1 : 1
Input data for node 2 : 2
Input data for node 3 : 3
Data entered in the list are :
node 1 : 1
node 2 : 2
node 3 : 3
After deletion the new list are :
node 1 : 1
node 2 : 2
20. Write a program in C to find the maximum value in a doubly linked list.
Test Data :
Input the number of nodes : 3
Input data for node 1 : 5
Input data for node 2 : 9
Input data for node 3 : 1
Expected Output :
Data entered in the list are :
node 1 : 5
node 2 : 9
node 3 : 1
The Maximum Value in the Linked List : 9
Click me to see the solution
21. Write a program in C to insert a node at any position in a circular linked list.
Test Data and Expected Output :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Data entered in the list are :
Data 1 = 2
Data 2 = 5
Data 3 = 8
Input the position to insert a new node : 3
Input data for the position 3 : 7
After insertion the new list are :
Data 1 = 2
Data 2 = 5
Data 3 = 7
Data 4 = 8
22. Write a program in C to delete a node from the middle of a circular linked list.
Test Data and Expected Output :
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
26. Write a C program to convert a doubly linked list into an array and return it.
Test Data and Expected Output :
Input the number of nodes: 4
Input data for node 1 : 10
Input data for node 2 : 11
Input data for node 3 : 12
Input data for node 4 : 13
Doubly linked list in array format:
10 11 12 13