Frequently Asked Linked List Interview Questions

We have carefully observed all Multi National Companies Interviews. We have chosen Commonly Asked Linked List Interview Questions  for you.

Q: Check if singly linked list is palindrome.

Answer:

Given a singly linked list, determine if its a palindrome. Return 1 or 0 denoting if its a palindrome or not, respectively.

For example: 1->2->1 O/P: 11->2->3O/P: 0

Q: Remove duplicates from the Linked list.

Answer:

Given an unsorted linked list, and without using a temporary buffer, write a method that will delete any duplicates from the linked list.

For example:  Linked List :1->2->3->4->2->4->1->5->5->NULLO/P:1->2->3->4->5->NULLLinked List: 2->1->4->2->5->3->3->5->NULLO/P: 2->1->4->5->3

Q: Find n’th node from end of the linked list.

Answer:

Given a linked list and a position from the end . Find the node at that position.

For example: Linked List: 1->2->5->3->7->12->4->9->5->NULLPosition-3O/p: 4( 3rd element from the last of the linked list.)

Q: Merge 2 sorted linked list.

Answer:

Given 2 linked list  sorted in increasing order. Write a function that merge the 2 linked list together into one linked list which in also inceasing.

For Example-Merge 1->1->3->6->8->9->NULL and 1->2->5->5->7->8->NULLO/p- 1->1->2->3->5->5->6->7->8->9->NULL

Q: Reverse the linked list in groups of given size.

Answer:

Given a linked list and a number k. Write a function to reverse every k node in the linked list.

For Example- 1->2->3->4->5->6->7->8->9->NULLK=4O/P:4->3->2->1->8->7->6->5->9->NULL

Q: Write a function to get the intersection point of two Linked Lists (Y Shape)

Answer:

There are 2 linked list which are forming Y shape in between . Write a function to get the intersection point of the 2 lists.

Q: Reverse the nodes of singly linked list.

Answer:

Given a linked list write a function that reverse the nodes of the linked list.

For example- 1->2->3->4->5->NULLO/p:  5->4->3->2->1->NULL

Q: Delete N nodes after M nodes of a linked list.

Answer:

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.

For Example-M=3, N=2Linked list: 1->2->3->4->5->6->7->8->9->NULLO/P: 1->2->3->6->7->8->NULLM=1, N=1Linked List 1->2->3->4->5->6->7->8->9->NULLO/P: 1->3->5->7->9->NULL

Q: Add 2 numbers represented by linked lists.

Answer:

Given 2 numbers represented by 2 linked list. Write a function that return a list as sum of the 2 numbers.

For example- List1: 2->4->9 ->NULL // represents number 946List 2: 8->5->-0->9->NULL // represents number 9058O/P: 4->0->0->0->1->NULL // represents number 10004

List 1: 5->6->1 // represents number 165
List 2: 3->4->5 // represents number 543
O/P: 8->0->7//represents 708

Q: Swap the elements of the linked list in pair.

Answer:

Given a linked list, write a function to swap elements in pair.

For example- Linked list: 1->2->3->4->5->6->-7->NULLO/P: 2->1->4->3->6->5->7->NULLLinked list: 1->2->3->4->5->6->NULLO/P: 2->1->4->3->6->5->NULL

Q: Merge sort for the linked lists.

Answer:

Merge sort in preferred in sorting the linked list because random access is not allowed in the linked list.

For Example- Linked list: 4->3->6->5->10->3->12->9->NULLO/P: 3->3->4->5->6->9->10->12->NULL

Q: Rearrange a linked list in Zig – Zag fashion.

Answer:

Given a linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f … where a, b, c… are consecutive data node of linked list.

For Example- Linked list: 11->14->19->7->10O/P: 11->19->7->14->10Linked list: 1->2->3->4O/P: 1->3->2->4

Q: Find pairs with given sum in doubly linked list.

Answer:

Given a sorted doubly linked list of positive distinct elements, the task is to find pairs in doubly linked list whose sum is equal to given value x, without using any extra space ?

Input : head : 1 <-> 2 <-> 4 <-> 5 <-> 6 <-> 8 <-> 9        x = 7Output: (6, 1), (5,2)

Q: Sort a linked list of 0s, 1s and 2s

Answer:

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

Q: Flattening a Linked List

Answer:

Given a linked list, in addition to the next pointer, each node has a child pointer that can point to a separate list. With the head node, flatten the list to a single-level linked list.


For instance, the above linked list should be flattened to 1→2->3→4->5->6->7->8->9. The idea is to flatten the linked list by level. Note: this question was asked by Facebook a month ago.

Q: Clone a linked list with next and random pointer

Answer:

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.

Q: Find loop in linked list and remove the loop

Answer:

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.

0--->1---->2---->3---->4---->5---->6                  ▲                 |                  |                 ▼                 11<—-22<—-12<—-9<—-8

Q: Count rotations in sorted and rotated linked list

Answer:

Given a linked list of n nodes which is first sorted, then rotated by k elements. Find the value of k.

Q: Union and intersection of 2 linked list

Answer:

iven two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. Order of elments in output lists doesn’t matter.

Example:

Input:   List1: 10->15->4->20   lsit2:  8->4->2->10Output:   Intersection List: 4->10   Union List: 2->8->20->4->15->10

Q: Segregate even and odd nodes in a Linked List

Answer:

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->NULLOutput: 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