Append the last n nodes of a linked list to the beginning of the list in Java

Java Program to Append the Last n Nodes of a Linked List

to the Beginning of the List

In this article, you will learn how to Append the Last n Nodes of a Linked List to the Beginning of the List in Java with clear explanations, step by step algorithms, and complete Java code examples.

Appending the last n nodes of a linked list to the beginning is a very common interview problem. This problem helps you understand list traversal, pointer movements, and list restructuring.

Append the Last n Nodes of a Linked List to the

Beginning of the List in Java

Problem Statement:

Given a singly linked list and a number n, your task is to cut the last n nodes from the end and attach them to the beginning of the list.

Input List:

1 → 2 → 3 → 4 → 5 → 6

If n = 2, then we take the last two nodes:

5 → 6

Final List becomes:

5 → 6 → 1 → 2 → 3 → 4

Explanation:

To solve Append the Last n Nodes of a Linked List to the Beginning of the List in Java, you mainly need:

1. Length Calculation:

Find the total number of nodes so you know where to split the list.

2. Identifying the Breaking Point:

If list has size L, then the breaking point is at the node number (L – n).
The last n nodes start from that point.

3. Reconnecting Nodes:

  • Detach the last n nodes
  • Attach them to the head of the list
  • Set the old tail’s next pointer to null
Append the last n node of a linked list to the beginning of the list

Methods to Append the Last n Nodes of a Linked List

to the Beginning of the List in Java

Here, we have mentioned 2 methods to Append the last n nodes of a linked list to the beginning of the list using Java:

Each includes algorithms, full Java code, examples, and complexities.

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Methods to Append the Last n Nodes of a Linked List

Method 1: Length + Pointer Movement (Iterative)

Algorithm:

  1. Let head be the starting node.
  2. If n == 0, return the original list.
  3. Traverse the list to calculate total length len.
  4. Set n = n % len (to handle n > len).
  5. If n == 0, return original list.
  6. Find the node just before the breaking position: index (len – n – 1).
  7. Let newHead be (len – n)th node.
  8. Traverse to the last node (tail).
  9. Connect tail.next to original head.
  10. Set the breaking node’s next to null.
  11. Return newHead.

Java Code:

Run
class AppendLastNNodes {

    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    public static Node appendLastN(Node head, int n) {
        if (head == null || n == 0) return head;

        // Find length
        int len = 0;
        Node temp = head;
        while (temp != null) {
            temp = temp.next;
            len++;
        }

        n = n % len;
        if (n == 0) return head;

        int breakPoint = len - n;
        Node curr = head;

        // Move to (breakPoint - 1)th node
        for (int i = 1; i < breakPoint; i++) {
            curr = curr.next;
        }

        Node newHead = curr.next;
        curr.next = null;

        Node tail = newHead;
        while (tail.next != null) {
            tail = tail.next;
        }

        tail.next = head;

        return newHead;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);

        System.out.println("Original List:");
        printList(head);

        head = appendLastN(head, 2);

        System.out.println("Modified List:");
        printList(head);
    }
}

Input:

1 2 3 4 5 6
n = 2

Output:

5 6 1 2 3 4

Methods to Append the Last n Nodes of a Linked List

Method 2: Two Pointer Fast and Slow Method

Algorithm:

  1. Let head be the start of the list.

  2. Use pointer fast and move it forward by n nodes.

  3. Use pointer slow starting at head.

  4. Move both fast and slow until fast reaches the last node.

  5. slow now points to the node before the last n nodes.

  6. Let newHead = slow.next.

  7. Set slow.next = null to break the list.

  8. Move fast to end (if not already there).

  9. Connect fast.next = head.

  10. Return newHead.

Java Code:

Run
class AppendLastNFastSlow {

    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    public static Node appendLastN(Node head, int n) {
        if (head == null || n == 0) return head;

        Node fast = head;
        int count = 0;

        while (count < n && fast != null) {
            fast = fast.next;
            count++;
        }

        if (fast == null) return head;

        Node slow = head;
        while (fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }

        Node newHead = slow.next;
        slow.next = null;
        fast.next = head;

        return newHead;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head.next.next.next = new Node(4);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);

        System.out.println("Original List:");
        printList(head);

        head = appendLastN(head, 3);

        System.out.println("Modified List:");
        printList(head);
    }
}

Input:

1 2 3 4 5 6
n = 3

Output:

4 5 6 1 2 3

What do we got is….

You learned how to Append the Last n Nodes of a Linked List to the Beginning of the List in Java using two efficient methods.

  • Both techniques run in linear time and constant space.
  • Understanding this improves your linked list manipulation skills and prepares you for interview level problems.

FAQ's related to Append the Last n Nodes of a Linked List in Java

Answer:

It means taking the last n nodes of a linked list and moving them to the front while keeping the order of the remaining nodes unchanged.

Answer:

We do this to practice pointer manipulation, linked list traversal, and to solve a common interview question based on list restructuring.

Answer:

Yes, the operation can be done in O(n) time and O(1) space using pointer-based approaches.

Answer:

Recursion is not commonly used for this problem because iterative pointer techniques are simpler and more efficient.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java