Given a Linkedlist and two indices A and B, the task is to print a sublist starting from A and ending at B.
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
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.
Below code is the implementation of the above approach:
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.
- Reverse a sublist of linked list
- Longest increasing sublist in a linked list
- Sublist Search (Search a linked list in another list)
- Print nodes of linked list at given indexes
- Print Reverse a linked list using Stack
- Print reverse of a Linked List without actually reversing
- An interesting method to print reverse of a linked list
- Print alternate nodes of a linked list using recursion
- Print Doubly Linked list in Reverse Order
- Print the alternate nodes of linked list (Iterative Method)
- Print reverse of a Linked List without extra space and modifications
- Print the last k nodes of the linked list in reverse order | Recursive approach
- Print the last k nodes of the linked list in reverse order | Iterative Approaches
- Create new linked list from two given linked list with greater element at each node
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- 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
- Convert Singly Linked List to XOR Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.