Loading

Quipoin Menu

Learn • Practice • Grow

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

Linked List Operations

Beyond basic insert and delete, linked lists have other common operations like finding length, searching for an element, and reversing the list.

1. Find Length (Iterative)


public int getLength() {
int count = 0;
Node temp = head;
while (temp != null) {
count++;
temp = temp.next;
}
return count;
}

2. Search for an element


public boolean search(int key) {
Node temp = head;
while (temp != null) {
if (temp.data == key) return true;
temp = temp.next;
}
return false;
}

3. Reverse a Singly Linked List (Iterative)
One of the most common interview problems.


public void reverse() {
Node prev = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next; // store next node
current.next = prev; // reverse link
prev = current; // move prev forward
current = next; // move current forward
}
head = prev;
}

4. Find Middle Node (Slow-Fast Pointer)


public int findMiddle() {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow.data;
}
Two Minute Drill
  • Length: O(n) traversal.
  • Search: O(n) linear scan.
  • Reverse: iterative with three pointers (prev, curr, next).
  • Middle: slow-fast pointer (tortoise and hare).
  • These operations are building blocks for more advanced problems.

Need more clarification?

Drop us an email at career@quipoinfotech.com