Top 30 Linked List
Pattern Questions
for DSA Interviews
Ace Your Product-Based
Company Interviews
with Java 8 Solutions
Compiled by Puneet Joshi for Software
Developers Preparing for Technical Interviews
Navigate to Contents
Jump to Questions
Table of Contents
1 Introduction 2
2 Linked List Node Definition 3
3 Questions and Solutions 4
3.1 Reverse a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Merge Two Sorted Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.3 Detect Cycle in a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.4 Find the Starting Point of a Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Remove Nth Node From End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.6 Middle of the Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.7 Palindrome Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.8 Add Two Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.9 Rotate List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.10 Swap Nodes in Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.11 Remove Duplicates from Sorted List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.12 Intersection of Two Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.13 Flatten a Linked List (Multilevel) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.14 Sort List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.15 Reverse Nodes in k-Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.16 Remove Linked List Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.17 Odd Even Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.18 Partition List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.19 Copy List with Random Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.20 Reorder List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.21 Find the Length of a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.22 Find the Kth Node From the Beginning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.23 Delete a Node with a Given Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.24 Reverse a Linked List Between Positions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.25 Split a Linked List into K Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.26 Merge K Sorted Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.27 Remove Duplicates from Unsorted List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.28 Find the Length of the Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.29 Insert into a Sorted Circular Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.30 Swap Nodes Without Swapping Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.31 Move Last Element to Front . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Created by Puneet Joshi
1 Introduction
This document provides the top 30 Linked List pattern questions frequently asked in Data Structures and
Algorithms (DSA) interviews at product-based companies. Each question includes a detailed problem state-
ment, a solution in Java 8, and an explanation of the approach. These patterns cover essential techniques like
two-pointers, fast-slow pointers, reversal, merging, and more, helping you build a strong foundation for Linked
List problems.
Created by Puneet Joshi
2 Linked List Node Defi-
nition
Below is the basic Linked List node class used in all solutions:
1 class ListNode {
2 int val ;
3 ListNode next ;
4 ListNode ( int val ) { this . val = val ; }
5 ListNode ( int val , ListNode next ) { this . val = val ; this . next = next ; }
6 }
Created by Puneet Joshi
3 Questions and Solutions
3.1 Reverse a Linked List
Problem: Reverse a singly linked list.
Solution: Use three pointers (prev, curr, next) to reverse the links iteratively.
1 public ListNode reverseList ( ListNode head ) {
2 ListNode prev = null , curr = head ;
3 while ( curr != null ) {
4 ListNode next = curr . next ;
5 curr . next = prev ;
6 prev = curr ;
7 curr = next ;
8 }
9 return prev ;
10 }
Explanation: We iterate through the list, reversing each node’s pointer to point to the previous node. Time
complexity: O(n), Space complexity: O(1).
Created by Puneet Joshi
3.2 Merge Two Sorted Lists
Problem: Merge two sorted linked lists into one sorted list.
Solution: Use a dummy node and compare nodes from both lists.
1 public ListNode mergeTwoLists ( ListNode l1 , ListNode l2 ) {
2 ListNode dummy = new ListNode (0) ;
3 ListNode curr = dummy ;
4 while ( l1 != null && l2 != null ) {
5 if ( l1 . val <= l2 . val ) {
6 curr . next = l1 ;
7 l1 = l1 . next ;
8 } else {
9 curr . next = l2 ;
10 l2 = l2 . next ;
11 }
12 curr = curr . next ;
13 }
14 curr . next = l1 != null ? l1 : l2 ;
15 return dummy . next ;
16 }
Explanation: Compare nodes and build the merged list. Time complexity: O(n + m), Space complexity:
O(1).
Created by Puneet Joshi
3.3 Detect Cycle in a Linked List
Problem: Determine if a linked list has a cycle.
Solution: Use Floyds Cycle-Finding Algorithm (fast-slow pointers).
1 public boolean hasCycle ( ListNode head ) {
2 ListNode slow = head , fast = head ;
3 while ( fast != null && fast . next != null ) {
4 slow = slow . next ;
5 fast = fast . next . next ;
6 if ( slow == fast ) return true ;
7 }
8 return false ;
9 }
Explanation: If fast and slow pointers meet, theres a cycle. Time complexity: O(n), Space complexity: O(1).
Created by Puneet Joshi
3.4 Find the Starting Point of a
Cycle
Problem: Find the node where the cycle begins in a linked list.
Solution: Use Floyds algorithm to detect the cycle, then find the entrance.
1 public ListNode detectCycle ( ListNode head ) {
2 ListNode slow = head , fast = head ;
3 boolean hasCycle = false ;
4 while ( fast != null && fast . next != null ) {
5 slow = slow . next ;
6 fast = fast . next . next ;
7 if ( slow == fast ) {
8 hasCycle = true ;
9 break ;
10 }
11 }
12 if (! hasCycle ) return null ;
13 slow = head ;
14 while ( slow != fast ) {
15 slow = slow . next ;
16 fast = fast . next ;
17 }
18 return slow ;
19 }
Explanation: After detecting a cycle, reset slow to head and move both pointers one step until they meet at
the cycles start. Time complexity: O(n), Space complexity: O(1).
Created by Puneet Joshi
3.5 Remove Nth Node From End
Problem: Remove the nth node from the end of a linked list.
Solution: Use two pointers with a gap of n nodes.
1 public ListNode removeNthFromEnd ( ListNode head , int n ) {
2 ListNode dummy = new ListNode (0 , head ) ;
3 ListNode slow = dummy , fast = dummy ;
4 for ( int i = 0; i <= n ; i ++) fast = fast . next ;
5 while ( fast != null ) {
6 slow = slow . next ;
7 fast = fast . next ;
8 }
9 slow . next = slow . next . next ;
10 return dummy . next ;
11 }
Explanation: Fast pointer moves n steps ahead, then both move until fast reaches the end. Slow points to
the node before the one to remove. Time complexity: O(n), Space complexity: O(1).
Created by Puneet Joshi
3.6 Middle of the Linked List
Problem: Find the middle node of a linked list.
Solution: Use fast-slow pointers.
1 public ListNode middleNode ( ListNode head ) {
2 ListNode slow = head , fast = head ;
3 while ( fast != null && fast . next != null ) {
4 slow = slow . next ;
5 fast = fast . next . next ;
6 }
7 return slow ;
8 }
Explanation: Fast moves twice as fast, so slow points to the middle when fast reaches the end. Time
complexity: O(n), Space complexity: O(1).
Created by Puneet Joshi
3.7 Palindrome Linked List
Problem: Check if a linked list is a palindrome.
Solution: Find the middle, reverse the second half, and compare.
1 public boolean isPalindrome ( ListNode head ) {
2 if ( head == null || head . next == null ) return true ;
3 ListNode slow = head , fast = head ;
4 while ( fast . next != null && fast . next . next != null ) {
5 slow = slow . next ;
6 fast = fast . next . next ;
7 }
8 ListNode secondHalf = reverseList ( slow . next ) ;
9 ListNode firstHalf = head ;
10 while ( secondHalf != null ) {
11 if ( firstHalf . val != secondHalf . val ) return false ;
12 firstHalf = firstHalf . next ;
13 secondHalf = secondHalf . next ;
14 }
15 return true ;
16 }
Explanation: Reverse the second half and compare with the first half. Time complexity: O(n), Space com-
plexity: O(1).
Created by Puneet Joshi
3.8 Add Two Numbers
Problem: Add two numbers represented as linked lists.
Solution: Iterate through both lists, summing digits and handling carry.
1 public ListNode addTwoNumbers ( ListNode l1 , ListNode l2 ) {
2 ListNode dummy = new ListNode (0) ;
3 ListNode curr = dummy ;
4 int carry = 0;
5 while ( l1 != null || l2 != null || carry != 0) {
6 int x = l1 != null ? l1 . val : 0;
7 int y = l2 != null ? l2 . val : 0;
8 int sum = x + y + carry ;
9 carry = sum / 10;
10 curr . next = new ListNode ( sum % 10) ;
11 curr = curr . next ;
12 l1 = l1 != null ? l1 . next : null ;
13 l2 = l2 != null ? l2 . next : null ;
14 }
15 return dummy . next ;
16 }
Explanation: Sum digits and propagate carry to the next position. Time complexity: O(max(n, m)), Space
complexity: O(max(n, m)).
Created by Puneet Joshi
3.9 Rotate List
Problem: Rotate a linked list to the right by k places.
Solution: Find the length, connect the list into a ring, and break at the right spot.
1 public ListNode rotateRight ( ListNode head , int k ) {
2 if ( head == null || head . next == null || k == 0) return head ;
3 ListNode curr = head ;
4 int length = 1;
5 while ( curr . next != null ) {
6 curr = curr . next ;
7 length ++;
8 }
9 curr . next = head ;
10 k = k % length ;
11 int stepsToNewHead = length - k ;
12 while ( stepsToNewHead - - > 0) curr = curr . next ;
13 ListNode newHead = curr . next ;
14 curr . next = null ;
15 return newHead ;
16 }
Explanation: Form a ring, then break it at the (length - k)th node. Time complexity: O(n), Space complexity:
O(1).
Created by Puneet Joshi
3.10 Swap Nodes in Pairs
Problem: Swap every two adjacent nodes in a linked list.
Solution: Swap pairs iteratively.
1 public ListNode swapPairs ( ListNode head ) {
2 if ( head == null || head . next == null ) return head ;
3 ListNode next = head . next ;
4 head . next = swapPairs ( next . next ) ;
5 next . next = head ;
6 return next ;
7 }
Explanation: Recursively swap each pair by adjusting pointers. Time complexity: O(n), Space complexity:
O(n) for recursion.
Created by Puneet Joshi
3.11 Remove Duplicates from Sorted
List
Problem: Remove duplicates from a sorted linked list.
Solution: Iterate and skip duplicates.
1 public ListNode deleteDuplicates ( ListNode head ) {
2 ListNode curr = head ;
3 while ( curr != null && curr . next != null ) {
4 if ( curr . val == curr . next . val ) {
5 curr . next = curr . next . next ;
6 } else {
7 curr = curr . next ;
8 }
9 }
10 return head ;
11 }
Explanation: Compare adjacent nodes and skip duplicates. Time complexity: O(n), Space complexity: O(1).
Created by Puneet Joshi
3.12 Intersection of Two Linked
Lists
Problem: Find the node where two linked lists intersect.
Solution: Use two pointers to align the lists.
1 public ListNode g et I n t er s e ct i o nN o d e ( ListNode headA , ListNode headB ) {
2 if ( headA == null || headB == null ) return null ;
3 ListNode p1 = headA , p2 = headB ;
4 while ( p1 != p2 ) {
5 p1 = p1 == null ? headB : p1 . next ;
6 p2 = p2 == null ? headA : p2 . next ;
7 }
8 return p1 ;
9 }
Explanation: Traverse both lists, switching heads when reaching the end to align pointers. Time complexity:
O(n + m), Space complexity: O(1).
Created by Puneet Joshi
3.13 Flatten a Linked List (Mul-
tilevel)
Problem: Flatten a multilevel linked list (nodes have a next and child pointer).
Solution: Use recursion to flatten.
1 class Node {
2 int val ;
3 Node next , child ;
4 Node ( int val ) { this . val = val ; }
5 }
6 public Node flatten ( Node head ) {
7 Node curr = head , tail = head ;
8 while ( curr != null ) {
9 Node child = curr . child , next = curr . next ;
10 if ( child != null ) {
11 curr . next = child ;
12 child . next = next ;
13 while ( child . next != null ) child = child . next ;
14 child . next = next ;
15 }
16 curr . child = null ;
17 curr = curr . next ;
18 }
19 return head ;
20 }
Explanation: For each node, merge its child list into the main list. Time complexity: O(n), Space complexity:
O(1).
Created by Puneet Joshi
3.14 Sort List
Problem: Sort a linked list in O(n log n) time.
Solution: Use merge sort.
1 public ListNode sortList ( ListNode head ) {
2 if ( head == null || head . next == null ) return head ;
3 ListNode slow = head , fast = head . next ;
4 while ( fast != null && fast . next != null ) {
5 slow = slow . next ;
6 fast = fast . next . next ;
7 }
8 ListNode second = slow . next ;
9 slow . next = null ;
10 ListNode left = sortList ( head ) ;
11 ListNode right = sortList ( second ) ;
12 return mergeTwoLists ( left , right ) ;
13 }
Explanation: Split the list, recursively sort, and merge. Time complexity: O(n log n), Space complexity:
O(log n).
Created by Puneet Joshi
3.15 Reverse Nodes in k-Group
Problem: Reverse every k nodes in a linked list.
Solution: Reverse k nodes at a time.
1 public ListNode reverseKGroup ( ListNode head , int k ) {
2 if ( head == null || k == 1) return head ;
3 int count = 0;
4 ListNode curr = head ;
5 while ( curr != null && count < k ) {
6 curr = curr . next ;
7 count ++;
8 }
9 if ( count < k ) return head ;
10 ListNode nextGroup = reverseKGroup ( curr , k ) ;
11 ListNode prev = null , current = head ;
12 for ( int i = 0; i < k ; i ++) {
13 ListNode next = current . next ;
14 current . next = prev ;
15 prev = current ;
16 current = next ;
17 }
18 head . next = nextGroup ;
19 return prev ;
20 }
Explanation: Reverse k nodes, then recurse for the rest. Time complexity: O(n), Space complexity: O(n/k).
Created by Puneet Joshi
3.16 Remove Linked List Elements
Problem: Remove all nodes with a given value.
Solution: Iterate and remove matching nodes.
1 public ListNode removeElements ( ListNode head , int val ) {
2 ListNode dummy = new ListNode (0 , head ) ;
3 ListNode curr = dummy ;
4 while ( curr . next != null ) {
5 if ( curr . next . val == val ) {
6 curr . next = curr . next . next ;
7 } else {
8 curr = curr . next ;
9 }
10 }
11 return dummy . next ;
12 }
Explanation: Use a dummy node to handle edge cases while removing nodes. Time complexity: O(n), Space
complexity: O(1).
Created by Puneet Joshi
3.17 Odd Even Linked List
Problem: Group all odd-positioned nodes followed by even-positioned nodes.
Solution: Split into odd and even lists, then merge.
1 public ListNode oddEvenList ( ListNode head ) {
2 if ( head == null ) return null ;
3 ListNode odd = head , even = head . next , evenHead = even ;
4 while ( even != null && even . next != null ) {
5 odd . next = even . next ;
6 odd = odd . next ;
7 even . next = odd . next ;
8 even = even . next ;
9 }
10 odd . next = evenHead ;
11 return head ;
12 }
Explanation: Maintain two sublists for odd and even positions, then connect them. Time complexity: O(n),
Space complexity: O(1).
Created by Puneet Joshi
3.18 Partition List
Problem: Partition a linked list around a value x, such that all nodes less than x come before nodes greater
than or equal to x.
Solution: Create two lists and merge.
1 public ListNode partition ( ListNode head , int x ) {
2 ListNode less = new ListNode (0) , greater = new ListNode (0) ;
3 ListNode lessCurr = less , greaterCurr = greater ;
4 while ( head != null ) {
5 if ( head . val < x ) {
6 lessCurr . next = head ;
7 lessCurr = lessCurr . next ;
8 } else {
9 greaterCurr . next = head ;
10 greaterCurr = greaterCurr . next ;
11 }
12 head = head . next ;
13 }
14 lessCurr . next = greater . next ;
15 greaterCurr . next = null ;
16 return less . next ;
17 }
Explanation: Split into two lists based on the value x, then merge. Time complexity: O(n), Space complexity:
O(1).
Created by Puneet Joshi
3.19 Copy List with Random Pointe
Problem: Copy a linked list where each node has a random pointer.
Solution: Interweave nodes, set random pointers, and separate.
1 class Node {
2 int val ;
3 Node next , random ;
4 Node ( int val ) { this . val = val ; }
5 }
6 public Node copyRandomList ( Node head ) {
7 if ( head == null ) return null ;
8 Node curr = head ;
9 while ( curr != null ) {
10 Node copy = new Node ( curr . val ) ;
11 copy . next = curr . next ;
12 curr . next = copy ;
13 curr = copy . next ;
14 }
15 curr = head ;
16 while ( curr != null ) {
17 Node copy = curr . next ;
18 copy . random = curr . random != null ? curr . random . next : null ;
19 curr = copy . next ;
20 }
21 Node dummy = new Node (0) , copyCurr = dummy ;
22 curr = head ;
23 while ( curr != null ) {
24 copyCurr . next = curr . next ;
25 copyCurr = copyCurr . next ;
26 curr . next = copyCurr . next ;
27 curr = curr . next ;
28 }
29 return dummy . next ;
30 }
Explanation: Create interweaved list, set random pointers, then separate. Time complexity: O(n), Space
complexity: O(1).
Created by Puneet Joshi
3.20 Reorder List
Problem: Reorder a linked list such that L0 Ln L1 Ln-1 L2 Ln-2.
Solution: Find middle, reverse second half, and merge.
1 public void reorderList ( ListNode head ) {
2 if ( head == null || head . next == null ) return ;
3 ListNode slow = head , fast = head ;
4 while ( fast . next != null && fast . next . next != null ) {
5 slow = slow . next ;
6 fast = fast . next . next ;
7 }
8 ListNode second = reverseList ( slow . next ) ;
9 slow . next = null ;
10 ListNode first = head ;
11 while ( second != null ) {
12 ListNode nextFirst = first . next , nextSecond = second . next ;
13 first . next = second ;
14 second . next = nextFirst ;
15 first = nextFirst ;
16 second = nextSecond ;
17 }
18 }
Explanation: Split, reverse the second half, and interleave with the first half. Time complexity: O(n), Space
complexity: O(1).
Created by Puneet Joshi
3.21 Find the Length of a Linked
List
Problem: Find the total number of nodes in a linked list.
Solution: Iterate through the list and count nodes.
1 public int getLength ( ListNode head ) {
2 int length = 0;
3 ListNode curr = head ;
4 while ( curr != null ) {
5 length ++;
6 curr = curr . next ;
7 }
8 return length ;
9 }
Explanation: Traverse the list and increment a counter for each node. Time complexity: O(n), Space com-
plexity: O(1).
Created by Puneet Joshi
3.22 Find the Kth Node From
the Beginning
Problem: Find the kth node from the start of a linked list (1-indexed).
Solution: Traverse k nodes from the head.
1 public ListNode findKthNode ( ListNode head , int k ) {
2 if ( head == null || k <= 0) return null ;
3 ListNode curr = head ;
4 for ( int i = 1; i < k && curr != null ; i ++) {
5 curr = curr . next ;
6 }
7 return curr ;
8 }
Explanation: Move k-1 steps from the head to reach the kth node. Time complexity: O(min(k, n)), Space
complexity: O(1).
Created by Puneet Joshi
3.23 Delete a Node with a Given
Key
Problem: Delete the first occurrence of a node with a given value.
Solution: Traverse the list and adjust pointers to skip the node.
1 public ListNode d eleteN odeWit hKey ( ListNode head , int key ) {
2 ListNode dummy = new ListNode (0 , head ) ;
3 ListNode curr = dummy ;
4 while ( curr . next != null ) {
5 if ( curr . next . val == key ) {
6 curr . next = curr . next . next ;
7 break ;
8 }
9 curr = curr . next ;
10 }
11 return dummy . next ;
12 }
Explanation: Use a dummy node to handle edge cases and skip the node with the given key. Time complexity:
O(n), Space complexity: O(1).
Created by Puneet Joshi
3.24 Reverse a Linked List Be-
tween Positions
Problem: Reverse a linked list from position m to n (1-indexed).
Solution: Isolate the sublist and reverse it.
1 public ListNode reverseBetween ( ListNode head , int m , int n ) {
2 if ( head == null || m == n ) return head ;
3 ListNode dummy = new ListNode (0 , head ) ;
4 ListNode prev = dummy ;
5 for ( int i = 0; i < m - 1; i ++) prev = prev . next ;
6 ListNode start = prev . next , then = start . next ;
7 for ( int i = 0; i < n - m ; i ++) {
8 start . next = then . next ;
9 then . next = prev . next ;
10 prev . next = then ;
11 then = start . next ;
12 }
13 return dummy . next ;
14 }
Explanation: Locate the sublist from m to n, reverse it, and reconnect it. Time complexity: O(n), Space
complexity: O(1).
Created by Puneet Joshi
3.25 Split a Linked List into K
Parts
Problem: Split a linked list into k consecutive parts of equal or nearly equal length.
Solution: Calculate part sizes and split accordingly.
1 public ListNode [] splitListToParts ( ListNode head , int k ) {
2 ListNode [] result = new ListNode [ k ];
3 int length = 0;
4 ListNode curr = head ;
5 while ( curr != null ) {
6 length ++;
7 curr = curr . next ;
8 }
9 int baseSize = length / k , extra = length % k ;
10 curr = head ;
11 for ( int i = 0; i < k ; i ++) {
12 ListNode partHead = curr , prev = null ;
13 int partSize = baseSize + ( i < extra ? 1 : 0) ;
14 for ( int j = 0; j < partSize ; j ++) {
15 prev = curr ;
16 curr = curr . next ;
17 }
18 if ( prev != null ) prev . next = null ;
19 result [ i ] = partHead ;
20 }
21 return result ;
22 }
Explanation: Calculate the size of each part, distribute extra nodes, and split the list. Time complexity:
O(n), Space complexity: O(k).
Created by Puneet Joshi
3.26 Merge K Sorted Lists
Problem: Merge k sorted linked lists into one sorted list.
Solution: Use a min-heap to merge efficiently.
1 public ListNode mergeKLists ( ListNode [] lists ) {
2 PriorityQueue < ListNode > pq = new PriorityQueue < >(( a , b ) -> a . val -
b . val ) ;
3 for ( ListNode node : lists ) {
4 if ( node != null ) pq . offer ( node ) ;
5 }
6 ListNode dummy = new ListNode (0) ;
7 ListNode curr = dummy ;
8 while (! pq . isEmpty () ) {
9 ListNode node = pq . poll () ;
10 curr . next = node ;
11 curr = curr . next ;
12 if ( node . next != null ) pq . offer ( node . next ) ;
13 }
14 return dummy . next ;
15 }
Explanation: Use a min-heap to always pick the smallest node from the k lists. Time complexity: O(n log
k), Space complexity: O(k).
Created by Puneet Joshi
3.27 Remove Duplicates from Un-
sorted List
Problem: Remove duplicates from an unsorted linked list.
Solution: Use a HashSet to track seen values.
1 public ListNode r e m o v e D u p l i c a t e s U n s o r t e d ( ListNode head ) {
2 if ( head == null || head . next == null ) return head ;
3 HashSet < Integer > seen = new HashSet < >() ;
4 ListNode curr = head , prev = null ;
5 while ( curr != null ) {
6 if ( seen . contains ( curr . val ) ) {
7 prev . next = curr . next ;
8 } else {
9 seen . add ( curr . val ) ;
10 prev = curr ;
11 }
12 curr = curr . next ;
13 }
14 return head ;
15 }
Explanation: Track seen values and skip duplicates. Time complexity: O(n), Space complexity: O(n).
Created by Puneet Joshi
3.28 Find the Length of the Cy-
cle
Problem: Find the length of a cycle in a linked list, if it exists.
Solution: Use Floyds algorithm to detect the cycle, then count the cycle length.
1 public int findCycleLength ( ListNode head ) {
2 ListNode slow = head , fast = head ;
3 boolean hasCycle = false ;
4 while ( fast != null && fast . next != null ) {
5 slow = slow . next ;
6 fast = fast . next . next ;
7 if ( slow == fast ) {
8 hasCycle = true ;
9 break ;
10 }
11 }
12 if (! hasCycle ) return 0;
13 int length = 1;
14 ListNode curr = slow . next ;
15 while ( curr != slow ) {
16 length ++;
17 curr = curr . next ;
18 }
19 return length ;
20 }
Explanation: After detecting a cycle, count the number of nodes in the loop. Time complexity: O(n), Space
complexity: O(1).
Created by Puneet Joshi
3.29 Insert into a Sorted Circu-
lar Linked List
Problem: Insert a value into a sorted circular linked list.
Solution: Find the correct position by comparing values.
1 class Node {
2 int val ;
3 Node next ;
4 Node ( int val ) { this . val = val ; }
5 }
6 public Node insert ( Node head , int insertVal ) {
7 Node newNode = new Node ( insertVal ) ;
8 if ( head == null ) {
9 newNode . next = newNode ;
10 return newNode ;
11 }
12 Node prev = head , curr = head . next ;
13 boolean inserted = false ;
14 do {
15 if ( prev . val <= insertVal && insertVal <= curr . val ) {
16 inserted = true ;
17 } else if ( prev . val > curr . val && ( insertVal >= prev . val ||
insertVal <= curr . val ) ) {
18 inserted = true ;
19 }
20 if ( inserted ) {
21 prev . next = newNode ;
22 newNode . next = curr ;
23 return head ;
24 }
25 prev = curr ;
26 curr = curr . next ;
27 } while ( prev != head ) ;
28 prev . next = newNode ;
29 newNode . next = curr ;
30 return head ;
31 }
Explanation: Traverse the circular list and insert the node in the correct position. Time complexity: O(n),
Space complexity: O(1).
Created by Puneet Joshi
3.30 Swap Nodes Without Swap-
ping Data
Problem: Swap two nodes in a linked list by adjusting pointers, not data.
Solution: Locate the nodes and adjust their pointers.
1 public ListNode swapNodes ( ListNode head , int x , int y ) {
2 if ( x == y ) return head ;
3 ListNode prevX = null , currX = head ;
4 while ( currX != null && currX . val != x ) {
5 prevX = currX ;
6 currX = currX . next ;
7 }
8 ListNode prevY = null , currY = head ;
9 while ( currY != null && currY . val != y ) {
10 prevY = currY ;
11 currY = currY . next ;
12 }
13 if ( currX == null || currY == null ) return head ;
14 if ( prevX != null ) prevX . next = currY ;
15 else head = currY ;
16 if ( prevY != null ) prevY . next = currX ;
17 else head = currX ;
18 ListNode temp = currX . next ;
19 currX . next = currY . next ;
20 currY . next = temp ;
21 return head ;
22 }
Explanation: Find the nodes with values x and y, then adjust their pointers to swap them. Time complexity:
O(n), Space complexity: O(1).
Created by Puneet Joshi
3.31 Move Last Element to Front
Problem: Move the last node of a linked list to the front.
Solution: Traverse to the second-to-last node and adjust pointers.
1 public ListNode moveLastToFront ( ListNode head ) {
2 if ( head == null || head . next == null ) return head ;
3 ListNode secondLast = head , last = head ;
4 while ( last . next != null ) {
5 secondLast = last ;
6 last = last . next ;
7 }
8 secondLast . next = null ;
9 last . next = head ;
10 return last ;
11 }
Explanation: Locate the last node, detach it, and attach it to the front. Time complexity: O(n), Space
complexity: O(1).
Created by Puneet Joshi