
Practice 50 Linked List Coding Questions
Q1. Write a Rust program to create a singly linked list and insert elements into it.
Input:
Elements: 1, 2, 3
Expected Output:
Linked List: 1 -> 2 -> 3
Q2. Write a Rust program to print the elements of a singly linked list.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
1 -> 2 -> 3
Q3. Write a Rust program to find the length of a singly linked list.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
Length of the list: 3
Q4. Write a Rust program to insert an element at the beginning of a singly linked list.
Input:
Linked List: 2 -> 3, Element to insert: 1
Expected Output:
Linked List: 1 -> 2 -> 3
Q5. Write a Rust program to insert an element at the end of a singly linked list.
Input:
Linked List: 1 -> 2, Element to insert: 3
Expected Output:
Linked List: 1 -> 2 -> 3
Q6. Write a Rust program to insert an element at a specified position in a singly linked list.
Input:
Linked List: 1 -> 3, Element to insert: 2, Position: 1
Expected Output:
Linked List: 1 -> 2 -> 3
Q7. Write a Rust program to delete an element from a singly linked list.
Input:
Linked List: 1 -> 2 -> 3, Element to delete: 2
Expected Output:
Linked List: 1 -> 3
Q8. Write a Rust program to delete the first element from a singly linked list.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
Linked List: 2 -> 3
Q9. Write a Rust program to delete the last element from a singly linked list.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
Linked List: 1 -> 2
Q10. Write a Rust program to reverse a singly linked list.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
Reversed Linked List: 3 -> 2 -> 1
Q11. Write a Rust program to find an element in a singly linked list.
Input:
Linked List: 1 -> 2 -> 3, Element to find: 2
Expected Output:
Element 2 found in the list.
Q12. Write a Rust program to check if a singly linked list is empty.
Input:
Linked List: {}
Expected Output:
The linked list is empty.
Q13. Write a Rust program to merge two singly linked lists.
Input:
Linked List 1: 1 -> 3 -> 5, Linked List 2: 2 -> 4 -> 6
Expected Output:
Merged Linked List: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Q14. Write a Rust program to detect a cycle in a singly linked list.
Input:
Linked List: 1 -> 2 -> 3 -> 4 (Cycle: 4 -> 2)
Expected Output:
Cycle detected in the linked list.
Q15. Write a Rust program to remove duplicates from a singly linked list.
Input:
Linked List: 1 -> 2 -> 2 -> 3
Expected Output:
Linked List without duplicates: 1 -> 2 -> 3
Q16. Write a Rust program to find the middle element of a singly linked list.
Input:
Linked List: 1 -> 2 -> 3 -> 4 -> 5
Expected Output:
Middle element: 3
Q17. Write a Rust program to find the nth element from the end of a singly linked list.
Input:
Linked List: 1 -> 2 -> 3 -> 4 -> 5, n = 2
Expected Output:
2nd element from the end: 4
Q18. Write a Rust program to sort the elements of a singly linked list in ascending order.
Input:
Linked List: 5 -> 1 -> 3 -> 2
Expected Output:
Sorted Linked List: 1 -> 2 -> 3 -> 5
Q19. Write a Rust program to clone a singly linked list.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
Cloned Linked List: 1 -> 2 -> 3
Q20. Write a Rust program to check if a singly linked list is a palindrome.
Input:
Linked List: 1 -> 2 -> 3 -> 2 -> 1
Expected Output:
The linked list is a palindrome.
Q21. Write a Rust program to find the intersection of two singly linked lists.
Input:
Linked List 1: 1 -> 2 -> 3 -> 4, Linked List 2: 3 -> 4 -> 5
Expected Output:
Intersection: 3 -> 4
Q22. Write a Rust program to find the union of two singly linked lists.
Input:
Linked List 1: 1 -> 2 -> 3, Linked List 2: 3 -> 4 -> 5
Expected Output:
Union: 1 -> 2 -> 3 -> 4 -> 5
Q23. Write a Rust program to create a linked list from an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Linked List: 1 -> 2 -> 3 -> 4
Q24. Write a Rust program to remove an element from a specific position in a singly linked list.
Input:
Linked List: 1 -> 2 -> 3 -> 4, Position to remove: 2
Expected Output:
Linked List after removal: 1 -> 3 -> 4
Q25. Write a Rust program to reverse a singly linked list using recursion.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
Reversed Linked List: 3 -> 2 -> 1
Q26. Write a Rust program to swap two nodes in a singly linked list.
Input:
Linked List: 1 -> 2 -> 3 -> 4, Swap nodes: 2 and 3
Expected Output:
Linked List after swapping: 1 -> 3 -> 2 -> 4
Q27. Write a Rust program to delete all nodes in a singly linked list.
Input:
Linked List: 1 -> 2 -> 3
Expected Output:
Linked List after deletion: {}
Q28. Write a Rust program to check if a singly linked list contains a cycle using Floyd's Tortoise and Hare algorithm.
Input:
Linked List: 1 -> 2 -> 3 -> 4 (Cycle: 4 -> 2)
Expected Output:
Cycle detected in the linked list using Floyd's algorithm.
Q29. Write a Rust program to find the intersection point of two singly linked lists.
Input:
Linked List 1: 1 -> 2 -> 3 -> 4, Linked List 2: 3 -> 4
Expected Output:
Intersection point: 3
Q30. Write a Rust program to implement a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3
Expected Output:
Doubly Linked List: 1 <-> 2 <-> 3
Q31. Write a Rust program to reverse a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3
Expected Output:
Reversed Doubly Linked List: 3 <-> 2 <-> 1
Q32. Write a Rust program to insert an element at the beginning of a doubly linked list.
Input:
Doubly Linked List: 2 <-> 3, Element to insert: 1
Expected Output:
Doubly Linked List: 1 <-> 2 <-> 3
Q33. Write a Rust program to insert an element at the end of a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2, Element to insert: 3
Expected Output:
Doubly Linked List: 1 <-> 2 <-> 3
Q34. Write a Rust program to delete an element from the beginning of a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3
Expected Output:
Doubly Linked List: 2 <-> 3
Q35. Write a Rust program to delete an element from the end of a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3
Expected Output:
Doubly Linked List: 1 <-> 2
Q36. Write a Rust program to delete a specific element from a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3, Element to delete: 2
Expected Output:
Doubly Linked List: 1 <-> 3
Q37. Write a Rust program to merge two doubly linked lists.
Input:
Doubly Linked List 1: 1 <-> 2, Doubly Linked List 2: 3 <-> 4
Expected Output:
Merged Doubly Linked List: 1 <-> 2 <-> 3 <-> 4
Q38. Write a Rust program to find the length of a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3
Expected Output:
Length of the doubly linked list: 3
Q39. Write a Rust program to clone a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3
Expected Output:
Cloned Doubly Linked List: 1 <-> 2 <-> 3
Q40. Write a Rust program to check if a doubly linked list is empty.
Input:
Doubly Linked List: {}
Expected Output:
The doubly linked list is empty.
Q41. Write a Rust program to check if two doubly linked lists are identical.
Input:
Doubly Linked List 1: 1 <-> 2 <-> 3, Doubly Linked List 2: 1 <-> 2 <-> 3
Expected Output:
The doubly linked lists are identical.
Q42. Write a Rust program to reverse a doubly linked list using recursion.
Input:
Doubly Linked List: 1 <-> 2 <-> 3
Expected Output:
Reversed Doubly Linked List: 3 <-> 2 <-> 1
Q43. Write a Rust program to find the middle element of a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3 <-> 4 <-> 5
Expected Output:
Middle element: 3
Q44. Write a Rust program to remove duplicates from a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 2 <-> 3
Expected Output:
Doubly Linked List without duplicates: 1 <-> 2 <-> 3
Q45. Write a Rust program to find an element in a doubly linked list.
Input:
Doubly Linked List: 1 <-> 2 <-> 3, Element to find: 2
Expected Output:
Element 2 found in the doubly linked list.
Q46. Write a Rust program to create a doubly linked list from an array.
Input:
Array: [1, 2, 3, 4]
Expected Output:
Doubly Linked List: 1 <-> 2 <-> 3 <-> 4
Q47. Write a Rust program to split a doubly linked list into two halves.
Input:
Doubly Linked List: 1 <-> 2 <-> 3 <-> 4 <-> 5
Expected Output:
First half: 1 <-> 2 <-> 3
Second half: 4 <-> 5
Q48. Write a Rust program to check if a doubly linked list contains a cycle.
Input:
Doubly Linked List: 1 <-> 2 <-> 3 <-> 4 (Cycle: 4 <-> 2)
Expected Output:
Cycle detected in the doubly linked list.
Q49. Write a Rust program to find the intersection of two doubly linked lists.
Input:
Doubly Linked List 1: 1 <-> 2 <-> 3 -> 4, Doubly Linked List 2: 3 <-> 4
Expected Output:
Intersection: 3 <-> 4
Q50. Write a Rust program to perform a union operation on two doubly linked lists.
Input:
Doubly Linked List 1: 1 <-> 2 -> 3, Doubly Linked List 2: 3 -> 4 <-> 5
Expected Output:
Union: 1 <-> 2 <-> 3 <-> 4 <-> 5