Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Jan 2, 2026

Describe your change:

Pairwise swap two nodes in a linked list in place

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • Documentation

    • Added comprehensive documentation for swapping adjacent nodes in singly linked lists, including detailed algorithm steps, illustrative diagrams, problem descriptions, and complexity analysis.
  • Improvements

    • Enhanced linked list node-swapping implementation with improved pointer manipulation approach for more efficient and reliable in-place node swapping.

✏️ Tip: You can customize this high-level summary in your review settings.

@BrianLusina BrianLusina self-assigned this Jan 2, 2026
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates LinkedList Linked List Data Structure labels Jan 2, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 2, 2026

📝 Walkthrough

Walkthrough

Methods for swapping adjacent nodes in a singly linked list are refactored: pairwise_swap_two() is renamed to pairwise_swap(), while the existing pairwise_swap() is renamed to pairwise_swap_with_modification(). The implementation is rewritten using dummy node and pointer manipulation. Documentation and tests are updated to reflect these changes.

Changes

Cohort / File(s) Summary
Documentation
datastructures/linked_lists/singly_linked_list/README.md
Added new documentation section "Swap Nodes in Pairs" describing the problem, constraints, solution approach, algorithm steps, diagrams, and complexity analysis.
Core Implementation
datastructures/linked_lists/singly_linked_list/single_linked_list.py
Renamed pairwise_swap()pairwise_swap_with_modification(); renamed and rewrote pairwise_swap_two()pairwise_swap() using dummy node with explicit pointer manipulations instead of value swaps.
Tests
datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py
Updated method calls from pairwise_swap_two() to pairwise_swap() and adjusted assertions to match new API signature.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A swap in the list, nodes danced with grace,
No values changed, just pointers in place,
From two to the main, the methods align,
A dummy node guides the pairs through the line! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: refactoring the pairwise swap implementation in linked lists data structures.
Description check ✅ Passed The description covers the main change (pairwise swap in place) and the author has completed most checklist items appropriately, indicating the PR modifies an existing algorithm with documentation updates.
Docstring Coverage ✅ Passed Docstring coverage is 83.33% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b3db38b and 383b875.

⛔ Files ignored due to path filters (13)
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_1.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_10.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_11.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_12.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_13.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_2.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_3.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_4.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_5.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_6.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_7.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_8.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_9.png is excluded by !**/*.png
📒 Files selected for processing (3)
  • datastructures/linked_lists/singly_linked_list/README.md
  • datastructures/linked_lists/singly_linked_list/single_linked_list.py
  • datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py
🧰 Additional context used
🧬 Code graph analysis (2)
datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py (2)
datastructures/linked_lists/singly_linked_list/single_linked_list.py (1)
  • pairwise_swap (757-793)
datastructures/linked_lists/singly_linked_list/node.py (1)
  • SingleNode (4-10)
datastructures/linked_lists/singly_linked_list/single_linked_list.py (2)
datastructures/linked_lists/singly_linked_list/node.py (1)
  • SingleNode (4-10)
datastructures/linked_lists/__init__.py (1)
  • pairwise_swap (521-534)
🪛 LanguageTool
datastructures/linked_lists/singly_linked_list/README.md

[style] ~640-~640: ‘overall structure’ might be wordy. Consider a shorter alternative.
Context: ...arrange the nodes while preserving the overall structure of the linked list. By adjusting the po...

(EN_WORDINESS_PREMIUM_OVERALL_STRUCTURE)

🔇 Additional comments (3)
datastructures/linked_lists/singly_linked_list/README.md (1)

622-685: Comprehensive documentation for the pairwise swap algorithm.

The documentation is well-structured with clear problem statement, constraints, solution approach, algorithm steps, visual diagrams, and complexity analysis.

Note: The AI summary mentions this section "appears twice in the diff (duplicate insertion)." If there is indeed a duplicate, please remove one instance to avoid redundancy.

datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py (1)

20-20: Test updates correctly reflect the new API.

The tests have been properly updated to call the renamed pairwise_swap() method and verify the returned head node for various cases (empty list, even-length list, and odd-length list).

Also applies to: 26-26, 35-35, 39-39

datastructures/linked_lists/singly_linked_list/single_linked_list.py (1)

727-755: Good refactoring - method name now clearly indicates it modifies node values.

The rename from pairwise_swap() to pairwise_swap_with_modification() makes it explicit that this method swaps node values rather than restructuring the node pointers.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
datastructures/linked_lists/singly_linked_list/single_linked_list.py (1)

757-793: Critical: self.head is not updated to the new head after swapping.

The implementation modifies self.head during the loop (line 791), but after the loop completes, self.head points to the node after the last swapped pair (or None), not the actual head of the swapped list. This leaves the SinglyLinkedList instance in an inconsistent state.

The method returns the correct new head (dummy_node.next), but self.head is not updated, which will cause subsequent operations on the linked list to fail or behave unexpectedly.

🔎 Proposed fix

Use a local variable current instead of mutating self.head during iteration, then update self.head at the end:

     def pairwise_swap(self) -> Optional[SingleNode]:
         """
         Swaps nodes in pairs without swapping the values in the nodes.
 
         Return:
             SingleNode: new head node or None if no head exists.
         """
         # nothing to do here
         if not self.head:
             return self.head
 
         # Create a dummy node with a value of None
         dummy_node = SingleNode(None)
         # This dummy node acts as the prev_node for the head node
         # of the list and hence stores pointer to the head node
         dummy_node.next = self.head
         prev_node = dummy_node
+        current = self.head
 
         # While the head node and the next node exist
-        while self.head and self.head.next:
+        while current and current.next:
             # nodes to be swapped
-            first_node = self.head
-            second_node = self.head.next
+            first_node = current
+            second_node = current.next
 
             # perform swapping of nodes
             # i. Set the previous node's next node to be the second node
             prev_node.next = second_node
             # ii. Set the first node's next node to be the second node's next node
             first_node.next = second_node.next
             # iii. Set the second node's next node to be the first node
             second_node.next = first_node
 
             # re-initialise the head and the previous node for next swap
             prev_node = first_node
-            self.head = first_node.next
+            current = first_node.next
 
+        # Update the instance head to the new head
+        self.head = dummy_node.next
         return dummy_node.next
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b3db38b and 383b875.

⛔ Files ignored due to path filters (13)
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_1.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_10.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_11.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_12.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_13.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_2.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_3.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_4.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_5.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_6.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_7.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_8.png is excluded by !**/*.png
  • datastructures/linked_lists/singly_linked_list/images/solutions/singly_linked_list_swap_nodes_in_pairs_solution_9.png is excluded by !**/*.png
📒 Files selected for processing (3)
  • datastructures/linked_lists/singly_linked_list/README.md
  • datastructures/linked_lists/singly_linked_list/single_linked_list.py
  • datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py
🧰 Additional context used
🧬 Code graph analysis (2)
datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py (2)
datastructures/linked_lists/singly_linked_list/single_linked_list.py (1)
  • pairwise_swap (757-793)
datastructures/linked_lists/singly_linked_list/node.py (1)
  • SingleNode (4-10)
datastructures/linked_lists/singly_linked_list/single_linked_list.py (2)
datastructures/linked_lists/singly_linked_list/node.py (1)
  • SingleNode (4-10)
datastructures/linked_lists/__init__.py (1)
  • pairwise_swap (521-534)
🪛 LanguageTool
datastructures/linked_lists/singly_linked_list/README.md

[style] ~640-~640: ‘overall structure’ might be wordy. Consider a shorter alternative.
Context: ...arrange the nodes while preserving the overall structure of the linked list. By adjusting the po...

(EN_WORDINESS_PREMIUM_OVERALL_STRUCTURE)

🔇 Additional comments (3)
datastructures/linked_lists/singly_linked_list/README.md (1)

622-685: Comprehensive documentation for the pairwise swap algorithm.

The documentation is well-structured with clear problem statement, constraints, solution approach, algorithm steps, visual diagrams, and complexity analysis.

Note: The AI summary mentions this section "appears twice in the diff (duplicate insertion)." If there is indeed a duplicate, please remove one instance to avoid redundancy.

datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py (1)

20-20: Test updates correctly reflect the new API.

The tests have been properly updated to call the renamed pairwise_swap() method and verify the returned head node for various cases (empty list, even-length list, and odd-length list).

Also applies to: 26-26, 35-35, 39-39

datastructures/linked_lists/singly_linked_list/single_linked_list.py (1)

727-755: Good refactoring - method name now clearly indicates it modifies node values.

The rename from pairwise_swap() to pairwise_swap_with_modification() makes it explicit that this method swaps node values rather than restructuring the node pointers.

@BrianLusina BrianLusina merged commit 599b271 into main Jan 3, 2026
6 of 8 checks passed
@BrianLusina BrianLusina deleted the feat/datastructures-linked-list-pairwise-swap branch January 3, 2026 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates enhancement LinkedList Linked List Data Structure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants