Linked List Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. Implement a function to reverse a singly linked list.
To reverse a linked list, you can iterate through the list and reverse the direction of pointers.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5
Output: 5 -> 4 -> 3 -> 2 -> 1
Ques 2. Detect a cycle in a linked list.
You can use Floyd's cycle-finding algorithm, also known as the 'tortoise and hare' algorithm.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 2 (cycle)
Output: True
Ques 3. Merge two sorted linked lists into a single sorted list.
Traverse both lists simultaneously, compare elements, and merge them into a new sorted list.
Example:
Input: List1: 1 -> 3 -> 5, List2: 2 -> 4 -> 6
Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Ques 4. Check if a linked list is a palindrome.
Reverse the second half of the list and compare it with the first half.
Example:
Input: 1 -> 2 -> 3 -> 2 -> 1
Output: True
Ques 5. Swap nodes in pairs in a linked list.
Iterate through the list and swap each pair of adjacent nodes.
Example:
Input: 1 -> 2 -> 3 -> 4
Output: 2 -> 1 -> 4 -> 3
Ques 6. Rotate a linked list by k places.
Find the length of the list, then move (length - k % length) steps to the right.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5, k = 2
Output: 4 -> 5 -> 1 -> 2 -> 3
Ques 7. Detect the intersection point of two linked lists.
Find the lengths of both lists, move the longer list's pointer ahead, and then iterate to find the intersection.
Example:
Input: List1: 1 -> 2 -> 3, List2: 6 -> 5 -> 2 -> 3
Output: Node with value 2
Most helpful rated by users: