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 firstname.lastname@example.org. 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.
- Longest increasing sublist in a linked list
- Sublist Search (Search a linked list in another list)
- Can we reverse a linked list in less than O(n)?
- Reverse a linked list
- Reverse first K elements of given linked list
- Reverse a Linked List according to its Size
- Reverse a circular linked list
- Reverse a Doubly Linked List | Set-2
- Reverse a Doubly Linked List
- Reverse each word in a linked list node
- Reverse a Linked List in groups of given size | Set 2
- Print reverse of a Linked List without actually reversing
- Program to reverse a linked list using Stack
- Reverse a doubly circular linked list
- Java Program for Reverse a linked list