We are given a linked list and positions m and n. We need to reverse the linked list from position m to n.
Input : 10->20->30->40->50->60->70->NULL m = 3, n = 6 Output : 10->20->60->50->40->30->70->NULL Input : 1->2->3->4->5->6->NULL m = 2, n = 4 Output : 1->4->3->2->5->6->NULL
To reverse the linked list from position m to n, we find addresses of start and end position of the linked list by running a loop, and then we unlink this part from the rest of the list and then use the normal linked list reverse function which we have earlier used for reversing the complete linked list, and use it to reverse the portion of the linked list which need to be reversed. After reversal, we again attach the portion reversed to the main list.
10 20 60 50 40 30 70
This article is contributed by Akshit Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Sublist Search (Search a linked list in another list)
- Longest increasing sublist in a linked list
- Print sublist of a given Linked List specified by given indices
- 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
- Create new linked list from two given linked list with greater element at each node
- Reverse a Doubly Linked List
- Reverse alternate K nodes in a Singly Linked List
- Given a linked list, reverse alternate nodes and append at the end
- Merge two sorted linked lists such that merged list is in reverse order
- Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
- Can we reverse a linked list in less than O(n)?
- An interesting method to print reverse of a linked list
- Reverse a Linked List in groups of given size | Set 2
- Reverse a Linked List according to its Size
Improved By : Akanksha_Rai