Skip to content

Commit 599b271

Browse files
authored
Merge pull request #138 from BrianLusina/feat/datastructures-linked-list-pairwise-swap
refactor(data-structures, linked-lists): pairwise swap
2 parents b3db38b + 383b875 commit 599b271

16 files changed

+97
-20
lines changed

datastructures/linked_lists/singly_linked_list/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,3 +618,68 @@ You don’t need to modify the values in the list’s nodes; only the links betw
618618
![Example 3](./images/examples/singly_linked_list_reorder_list_example_3.png)
619619
![Example 4](./images/examples/singly_linked_list_reorder_list_example_4.png)
620620
![Example 5](./images/examples/singly_linked_list_reorder_list_example_5.png)
621+
622+
---
623+
## Swap Nodes in Pairs
624+
625+
Given a singly linked list, swap every two adjacent nodes of the linked list. After the swap, return the head of the
626+
linked list.
627+
628+
Note: Solve the problem without modifying the values in the list’s nodes. In other words, only the nodes themselves can
629+
be changed.
630+
631+
### Constraints
632+
633+
- The number of nodes in the list is in the range [0,100]
634+
- 0 <= `node.value` <= 100
635+
636+
### Solution
637+
638+
The essence of this solution lies in understanding that we need to swap nodes in pairs without altering the values within
639+
the nodes. To accomplish this, we use an in-place reversal approach, manipulating pointers to rearrange the nodes while
640+
preserving the overall structure of the linked list. By adjusting the pointers of each node, we can efficiently swap the
641+
nodes in place in linear time without needing extra space or altering the data. This method ensures that the linked list
642+
remains intact and correctly ordered, with each pair of nodes swapped seamlessly as we progress through the list.
643+
644+
The algorithm for this problem is as follows:
645+
646+
- Initialize a dummy node that would reference the first node of the linked list, which is the head of the linked list.
647+
We’ll use prev_node to update the pointer of the dummy node.
648+
649+
- Next, we iterate over the linked list and swap the pairs of nodes as we proceed. The two nodes to be swapped can be
650+
represented as first_node and second_node.
651+
652+
These steps are represented in the following illustration:
653+
654+
![Solution 1](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_1.png)
655+
![Solution 2](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_2.png)
656+
![Solution 3](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_3.png)
657+
![Solution 4](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_4.png)
658+
![Solution 5](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_5.png)
659+
660+
- After the swap, update the position of the previous node and the head node for the next swap.
661+
- Reinitialize first_node to prev_node to ensure that the nodes are swapped on the go and attached to the previously
662+
swapped list. Ultimately, we’ll get the final reversed pairs of linked lists.
663+
664+
> Note: Ultimately, we have to return the second node because, at the end of the final iteration, the second node of the
665+
> list would have technically become the first node of the list.
666+
> The reference of the first node is secured by the previous node in the first swap when we assign prev_node = first_node.
667+
668+
The following illustration shows these steps in detail:
669+
670+
![Solution 6](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_6.png)
671+
![Solution 7](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_7.png)
672+
![Solution 8](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_8.png)
673+
![Solution 9](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_9.png)
674+
![Solution 10](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_10.png)
675+
![Solution 11](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_11.png)
676+
![Solution 12](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_12.png)
677+
![Solution 13](./images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_13.png)
678+
679+
#### Time Complexity
680+
681+
The time complexity of the solution above is O(n), where n is the number of nodes in the linked list.
682+
683+
#### Space Complexity
684+
685+
The space complexity of the solution above is O(1).
49.9 KB
Loading
47.1 KB
Loading
46.2 KB
Loading
44.5 KB
Loading
32.6 KB
Loading
43.6 KB
Loading
49.1 KB
Loading
43.6 KB
Loading
43.4 KB
Loading

0 commit comments

Comments
 (0)