Given a Linkedlist and two indices **A** and **B**, the task is to print a sublist starting from **A** and ending at ** B**.

**Examples:**

Input:list = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL, A = 3, B = 9

Output:3 4 5 6 7 8 9

Input:list = 1 -> 3 -> 4 -> 10 -> NULL, A = 2, B = 2

Output:3

**Approach:**

Follow the steps below to solve the problem:

- Create three instance variables:
**current:**Head of the given linked list**subcurrent:**Head of the sublist**subend:**Tail of the sublist.

- Traverse the linked list and initialize an
**index_count**variable which increments after each iteration. - Set
**subcurrent**as the node being currently pointed when the value of the**index_count**is equal to A. - If
**index_count**is equal to B, assign**subend**to the current node being referenced. Point**subend.next**to*NULL*to denote the end of the sublist. - Print the list contents from
**subcurrent**to**subend**.

Below code is the implementation of the above approach:

## Java

`// Java Program to find the ` `// subList in a linked list ` ` ` `import` `java.util.Scanner; ` ` ` `// Class representing the ` `// structure of a Linked List ` `public` `class` `LinkedListSublist { ` ` ` `Node head; ` ` ` `class` `Node { ` ` ` `int` `data; ` ` ` `Node next = ` `null` `; ` ` ` `Node(` `int` `new_data) ` ` ` `{ ` ` ` `data = new_data; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Function to push node ` ` ` `// at beginning of a ` ` ` `// Linked List ` ` ` `public` `void` `pushNode(` `int` `new_data) ` ` ` `{ ` ` ` `Node new_node = ` `new` `Node(new_data); ` ` ` `new_node.next = head; ` ` ` `head = new_node; ` ` ` `} ` ` ` ` ` `// Function to find sublist ` ` ` `public` `Node subList(Node head, ` ` ` `int` `A, ` ` ` `int` `B) ` ` ` `{ ` ` ` `Node subcurrent = ` `null` `; ` ` ` `Node subend = ` `null` `; ` ` ` `Node current = head; ` ` ` `int` `i = ` `1` `; ` ` ` ` ` `// traverse between indices ` ` ` `while` `(current != ` `null` ` ` `&& i <= B) { ` ` ` ` ` `// If the starting index ` ` ` `// of the sublist is found ` ` ` `if` `(i == A) { ` ` ` `subcurrent = current; ` ` ` `} ` ` ` ` ` `// If the ending index of ` ` ` `// the sublist is found ` ` ` `if` `(i == B) { ` ` ` `subend = current; ` ` ` `subend.next = ` `null` `; ` ` ` `} ` ` ` ` ` `// Move to next node ` ` ` `current = current.next; ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Return the head ` ` ` `// of the sublist ` ` ` `return` `subcurrent; ` ` ` `} ` ` ` ` ` `// Function to print ` ` ` `// the linked list ` ` ` `public` `void` `traversing() ` ` ` `{ ` ` ` `Node current = head; ` ` ` `while` `(current != ` `null` `) { ` ` ` `System.out.print(current.data ` ` ` `+ ` `" -> "` `); ` ` ` `current = current.next; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `LinkedListSublist list ` ` ` `= ` `new` `LinkedListSublist(); ` ` ` `int` `N = ` `1` `; ` ` ` `int` `value = ` `10` `; ` ` ` ` ` `while` `(N < ` `11` `) { ` ` ` `list.pushNode(value--); ` ` ` `N++; ` ` ` `} ` ` ` ` ` `// Starting index ` ` ` `int` `A = ` `3` `; ` ` ` ` ` `// Ending index ` ` ` `int` `B = ` `9` `; ` ` ` ` ` `list.head ` ` ` `= list.subList( ` ` ` `list.head, A, B); ` ` ` `list.traversing(); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 ->

**Time Complexity:** O(N)

**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sublist Search (Search a linked list in another list)
- Reverse a sublist of linked list
- Longest increasing sublist in a linked list
- Create new linked list from two given linked list with greater element at each node
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Print nodes of linked list at given indexes
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Construct a Doubly linked linked list from 2D Matrix
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Print reverse of a Linked List without actually reversing
- An interesting method to print reverse of a linked list
- Print reverse of a Linked List without extra space and modifications
- Print Reverse a linked list using Stack

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.