-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(datastructure): linked list pair wise swap
- Loading branch information
1 parent
446ff62
commit 00e2ce8
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
datastructures/linked_lists/singly_linked_list/test_singly_linked_list_pairwise_swap.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import unittest | ||
|
||
from . import SinglyLinkedList, SingleNode | ||
|
||
|
||
class SinglyLinkedListPairwiseTest(unittest.TestCase): | ||
def test_empty_list(self): | ||
linked_list = SinglyLinkedList() | ||
head = linked_list.pairwise_swap() | ||
self.assertIsNone(head) | ||
|
||
def test_two(self): | ||
"""1 -> 2 -> 3 -> 4 should become 2 -> 1 -> 4 -> 3""" | ||
linked_list = SinglyLinkedList() | ||
linked_list.append(1) | ||
linked_list.append(2) | ||
linked_list.append(3) | ||
linked_list.append(4) | ||
|
||
head = linked_list.pairwise_swap_two() | ||
|
||
expected_head = SingleNode(2, next_=SingleNode(1, next_=SingleNode(4, next_=SingleNode(3)))) | ||
|
||
self.assertEqual(head, expected_head) | ||
|
||
def test_three(self): | ||
"""7 -> 2 -> 1 should become 2 -> 7 -> 1""" | ||
linked_list = SinglyLinkedList() | ||
linked_list.append(7) | ||
linked_list.append(2) | ||
linked_list.append(1) | ||
|
||
head = linked_list.pairwise_swap_two() | ||
|
||
expected_head = SingleNode(2, next_=SingleNode(7, next_=SingleNode(1))) | ||
|
||
self.assertEqual(head, expected_head) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |