Loading

Quipoin Menu

Learn • Practice • Grow

data-structure-with-java / Linked List Problems
tutorial

Linked List Problems

Now let's solve classic linked list problems that frequently appear in interviews.

1. Detect if Linked List is Palindrome
Approach: Find middle, reverse second half, compare.


public boolean isPalindrome() {
// Find middle
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// Reverse second half
Node prev = null;
Node curr = slow;
while (curr != null) {
Node next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
// Compare first half and reversed second half
Node first = head;
Node second = prev;
while (second != null) {
if (first.data != second.data) return false;
first = first.next;
second = second.next;
}
return true;
}

2. Merge Two Sorted Lists
Similar to merging arrays, but with pointers.


public static Node mergeSorted(Node l1, Node l2) {
Node dummy = new Node(0);
Node tail = dummy;
while (l1 != null && l2 != null) {
if (l1.data < l2.data) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}
tail.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
Two Minute Drill
  • Palindrome: find middle, reverse second half, compare.
  • Merge sorted lists: use dummy head to simplify.
  • These problems test pointer manipulation and recursion skills.
  • Commonly asked in interviews.

Need more clarification?

Drop us an email at career@quipoinfotech.com