Skip to content

Commit dd76888

Browse files
committed
docs(data-structures, linked-list): add solution description for reverse sublist of linked list
1 parent 5b7366c commit dd76888

14 files changed

+89
-3
lines changed

datastructures/linked_lists/singly_linked_list/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,84 @@ The time complexity of the solution above is O(n), where n is the number of node
683683
#### Space Complexity
684684

685685
The space complexity of the solution above is O(1).
686+
687+
---
688+
689+
## Reverse Nodes in a Linked List Between left and right
690+
691+
Given a singly linked list with n nodes and two positions, left and right, the objective is to reverse the nodes of the
692+
list from left to right. Return the modified list.
693+
694+
### Constraints
695+
696+
- 1 ≤ n ≤ 500
697+
- -5000 ≤ `node.value` ≤ 5000
698+
- 1 ≤ `left``right` ≤ n
699+
700+
### Solution
701+
702+
The essence of this algorithm lies in refining the process of reversing specific sublists within a linked list by
703+
directly adjusting the pointer of the nodes, ensuring efficient in-place manipulation without using additional memory.
704+
It begins with the initialization of a dummy node, placed before the head of the list, to simplify edge cases, such as
705+
sublist reversals starting from the first node of the list. The algorithm starts by linking the dummy node to the head
706+
of the list and iterating the list until reaching the node immediately preceding the sublist to be reversed, marking it
707+
as the previous node. Then, for each node in the sublist, it moves that node to the front of the sublist and connects it
708+
to the previous node, effectively reversing its order and seamlessly integrating the reversed sublist back into the main
709+
list. The algorithm returns the dummy next as the head of the newly reversed linked list.
710+
711+
Now, let’s look at this algorithm in detail:
712+
713+
This optimized approach directly modifies the pointers of the nodes within the linked list. It achieves this by carefully
714+
tracking the sublist’s current, next, and previous nodes within the sublist to be reversed.
715+
716+
The algorithm steps are given below:
717+
718+
- We initialize a dummy node, which will be helpful in scenarios where the reversal of the sublist starts from the head
719+
of the list.
720+
- We set the next node of dummy to point to the head of the list.
721+
- We initialize a pointer, prev, to the dummy node. This pointer will help us reconnect the sublist to the entire list
722+
after it has been reversed.
723+
- We use a loop to traverse the list with the prev pointer and until it reaches the node immediately before the sublist
724+
to be reversed.
725+
- We initialize a curr pointer, which points to the node next to prev. This will point to the 'head' node of the sub list
726+
to be reversed
727+
- Another loop is used to reverse the sublist. This loop iterates right - left times, which is the number of nodes in the
728+
sublist minus one:
729+
- We set next_node to curr.next, representing the node to be moved to the front of the reversed sublist.
730+
- We update curr.next to next_node.next, effectively removing next_node from its current position in the sublist.
731+
- We set next_node.next to prev.next, inserting next_node at the beginning of the reversed sublist.
732+
- We update prev.next to next_node, adjusting the pointer to next_node for the next iteration.
733+
- Finally, we return dummy.next, which is the head of the modified linked list.
734+
735+
![Solution 1](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_1.png)
736+
![Solution 2](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_2.png)
737+
![Solution 3](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_3.png)
738+
![Solution 4](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_4.png)
739+
![Solution 5](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_5.png)
740+
![Solution 6](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_6.png)
741+
![Solution 7](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_7.png)
742+
![Solution 8](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_8.png)
743+
![Solution 9](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_9.png)
744+
![Solution 10](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_10.png)
745+
![Solution 11](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_11.png)
746+
![Solution 12](./images/solutions/singly_linked_list_reverse_between_left_and_right_solution_12.png)
747+
748+
#### Solution Summary
749+
750+
To recap, the solution to this problem can be divided into the following five main steps:
751+
752+
- We initialize a dummy node and link it to the head of the linked list.
753+
- We locate the node preceding the sublist to be reversed by traversing the list.
754+
- We set a pointer to the starting node of the sublist to be reversed.
755+
- Within a loop, we iteratively reverse the sublist by adjusting pointers, moving one node at a time.
756+
- We return the head of the reversed sublist, ensuring the original list remains intact.
757+
758+
#### Time Complexity
759+
760+
The time complexity of this solution is O(n), where n is the number of nodes in the linked list. This is because each
761+
node will be processed at most one time.
762+
763+
#### Space Complexity
764+
765+
The space complexity of this solution is O(1), since we are using a constant number of additional variables to maintain
766+
the connections between the nodes during the reversal process.
43.1 KB
Loading
Loading
50.2 KB
Loading
Loading
46.2 KB
Loading
51.6 KB
Loading
54.6 KB
Loading
51.5 KB
Loading
51.9 KB
Loading

0 commit comments

Comments
 (0)