Foundation·Concept·2 min read

Linked list in Java

Practice this question

Node structure

java
class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
}

Common Java questions

  • Reverse Linked List
  • Middle of the Linked List
  • Remove Nth Node from End
  • Merge Two Sorted Lists
  • Detect Cycle
  • Intersection of Two Linked Lists

Pattern: reverse linked list

java
ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode curr = head;

    while (curr != null) {
        ListNode next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

Interview guidance

Always handle null, single node, and last node carefully.


Adapted from 15_DSA_JAVA.md · “4. Linked list in Java”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.