LinkedList Common Problems and Solutions
This is my second blog on LinkedLists. In this blog, some of the most common problems and their
solutions are discussed. You can also read about types of linked lists and implementations in Java in
my prvious post, LinkedList Implementation and Basic Operations. To make a best use of this post,
please read the question first and do little brainstorm before moving on to the solution.
You can also try to implement solution once you are familiar with solution idea.
a) The easy solution is adding all elements of the first linked list into Hashset and checking one by one if an element of the second linked list is contained in the hashset.
Time Complexity is O(n+m) and Space Complecity is O(n) b) We iterate both lists and get length of each list. Then move pointer k steps forward on the longer list where k is the difference between lengths of lists. Now we need to move two pointers in both lists starting from head element in shorter list and kth element in longer list. We can find out intersection point the lists when two pointers meet at some point.
Time Complexity is O(max(m,n)) and Space Complecity is O(1) c) We maintain two pointers x and y at the head of A and B lists respectively. Then we move pointers one node at a time. When x reaches the end of a list, then redirect it to the head of B, similarly when y reaches the end of a list, redirect it the head of A. If at any point x meets y, then x/y is the intersection node.
Time Complexity is O(m+n) and Space Complecity is O(1) Below code snippet shows the third solution.
a) We iterate the list and add every node into Hashset, if element is already in the set we can return this element as cycle starting point.
Time Complexity is O(n) and Space Complecity is O(n) b) This solution is combination of the first and the second problem solutions. We first find any point in the cycle if there is. Then break the list by setting next pointer of node to null and find intersection point of two lists. The intersection point is the point where cycle begins.
Time Complexity is O(n+m) and Space Complecity is O(1)
a) We can reverse singly linked list in two ways, using recursion and iteration. The idea is to recur until tail node is encountered and set current node as next element of returned element in recursion.
We can use merge sort to sort linked list here. First we need to find mid element of the linked list using two pointers as mentioned above. Then, we should break the link by setting null to the next of mid element. In this case we can have have two lists; left linked list starts from head to mid and right linked list that starts from mid+1 to end of the linked list. We divide linked list until it has only one element in both left and right lists. Then starts merging of left and right lists into one sorted list. You can find solution below in Java These are some other commonly asked questions in interviews.
1) Given a singly linked list, determine if it has a cycle in it.Solution: a) We iterate the list and add each element into the hashtable until current element is null or current element is already in the hashtable. This solution uses O(N) space to store list elements in hashtable. Time complexity is also O(N). b) We can use two pointers: slow and fast. slow pointer moves step by step while fast pointer moves two steps at a time. If the linked list has a cycle slow and fast pointers meet at some point. This solution uses O(1) space, which is better than previous solution. Here is implementation of solution in Java.
2) Given two singly linked lists, find the node at which the intersection of these lists begins. Below given illustration of linked lists with intersectionSolution:
a) The easy solution is adding all elements of the first linked list into Hashset and checking one by one if an element of the second linked list is contained in the hashset.
Time Complexity is O(n+m) and Space Complecity is O(n) b) We iterate both lists and get length of each list. Then move pointer k steps forward on the longer list where k is the difference between lengths of lists. Now we need to move two pointers in both lists starting from head element in shorter list and kth element in longer list. We can find out intersection point the lists when two pointers meet at some point.
Time Complexity is O(max(m,n)) and Space Complecity is O(1) c) We maintain two pointers x and y at the head of A and B lists respectively. Then we move pointers one node at a time. When x reaches the end of a list, then redirect it to the head of B, similarly when y reaches the end of a list, redirect it the head of A. If at any point x meets y, then x/y is the intersection node.
Time Complexity is O(m+n) and Space Complecity is O(1) Below code snippet shows the third solution.
3) Given a linked list, return the node where the cycle begins without modifying the list. If there is no cycle, return null.Solution:
a) We iterate the list and add every node into Hashset, if element is already in the set we can return this element as cycle starting point.
Time Complexity is O(n) and Space Complecity is O(n) b) This solution is combination of the first and the second problem solutions. We first find any point in the cycle if there is. Then break the list by setting next pointer of node to null and find intersection point of two lists. The intersection point is the point where cycle begins.
Time Complexity is O(n+m) and Space Complecity is O(1)
4) Reverse a singly linked list.Solution:
a) We can reverse singly linked list in two ways, using recursion and iteration. The idea is to recur until tail node is encountered and set current node as next element of returned element in recursion.
5) Sort a singly linked list in O(n log n) time using constant space complexity.Solution:
We can use merge sort to sort linked list here. First we need to find mid element of the linked list using two pointers as mentioned above. Then, we should break the link by setting null to the next of mid element. In this case we can have have two lists; left linked list starts from head to mid and right linked list that starts from mid+1 to end of the linked list. We divide linked list until it has only one element in both left and right lists. Then starts merging of left and right lists into one sorted list. You can find solution below in Java These are some other commonly asked questions in interviews.
- Remove Duplicates from Sorted List
- Palindrome Linked List
- Rotate List
- Add Two Numbers
- Merge k Sorted Lists