Java Notes
Complete guide to Linked Lists — Singly, Doubly, and Circular linked lists with Java implementations, operations, and classic interview problems.
What is a Linked List?
A linked list is a linear data structure where elements (nodes) are stored in non-contiguous memory, connected via pointers/references.
Singly Linked List
[10|→] → [20|→] → [30|→] → [40|null]
head tail
Array vs Linked List
Array: [10][20][30][40] — contiguous memory, O(1) access
List: [10]→[20]→[30]→[40] — scattered memory, O(n) access
| Operation | Array | Linked List |
|---|---|---|
| Access by index | O(1) | O(n) |
| Insert at head | O(n) | O(1) |
| Insert at tail | O(1)* | O(1) with tail pointer |
| Insert at middle | O(n) | O(1) if at node |
| Delete | O(n) | O(1) if at node |
| Search | O(n) | O(n) |
Doubly Linked List
Classic Interview Problems
// Merge two sorted linked lists
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) { curr.next = l1; l1 = l1.next; }
else { curr.next = l2; l2 = l2.next; }
curr = curr.next;
}
curr.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
// Check if palindrome
public boolean isPalindrome(ListNode head) {
// Find middle, reverse second half, compare
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; fast = fast.next.next;
}
// Reverse from slow
ListNode prev = null;
while (slow != null) {
ListNode next = slow.next;
slow.next = prev; prev = slow; slow = next;
}
// Compare
while (prev != null) {
if (head.val != prev.val) return false;
head = head.next; prev = prev.next;
}
return true;
}Interview Questions
Q1: When would you use a Linked List over an ArrayList?
Answer: When frequent insertions/deletions at the beginning or middle are needed, when size is unpredictable (no resizing), or implementing stacks/queues. ArrayList is better for random access and iteration (cache-friendly).
Q2: How do you detect a cycle in a linked list?
Answer: Floyd's Tortoise and Hare: use two pointers — slow moves 1 step, fast moves 2 steps. If they meet, there's a cycle. If fast reaches null, no cycle. Time: O(n), Space: O(1).
Q3: How do you find the start of a cycle?
Answer: After detecting the cycle (slow meets fast), reset one pointer to head. Move both at speed 1. Where they meet again is the cycle start. Mathematical proof: distance from head to cycle start equals distance from meeting point to cycle start.
Q4: How do you reverse a linked list in O(1) space?
Answer: Iterative: maintain three pointers (prev, curr, next). At each step: save next, point curr.next to prev, advance prev to curr, advance curr to next. After loop, head = prev.
Q5: What is the time complexity to access the middle element?
Answer: O(n) — must traverse from head. Use the fast/slow pointer technique: fast moves 2x speed, when fast reaches end, slow is at middle.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Linked List in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, dsa, linked, list
Related Java Master Course Topics