Skip to content

Commit 7af3b49

Browse files
committed
feat(linkedlist): doubly linked list move tail to head
1 parent a49c8b3 commit 7af3b49

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

datastructures/linked_lists/doubly_linked_list/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,28 @@ def move_to_front(self, node: DoubleNode):
581581
self.head.next = self.tail
582582
self.tail.previous = self.head
583583

584+
def move_tail_to_head(self):
585+
if self.head and self.head.next:
586+
last = self.head
587+
588+
while last.next:
589+
last = last.next
590+
591+
# we can obtain the second_to_last node from the last node
592+
second_to_last = last.previous
593+
594+
# set the current head node's second_to_last pointer to the last node
595+
self.head.previous = last
596+
# set the next pointer of the last node to the head node
597+
last.next = self.head
598+
# remove the second_to_last pointer of the last node
599+
last.previous = None
600+
# remove the next pointer of the second_to_last node
601+
second_to_last.next = None
602+
603+
# set the head node as the last node
604+
self.head = last
605+
584606
def remove_tail(self):
585607
current = self.head
586608

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import unittest
2+
3+
from . import DoublyLinkedList
4+
5+
6+
class DoublyLinkedListMoveTailToHeadTests(unittest.TestCase):
7+
def test_1(self):
8+
"""should move tail of ["r", "a", "c", "e", "c", "a", "r"] to become ["r", "r", "a", "c", "e", "c", "a"]"""
9+
linked_list = DoublyLinkedList()
10+
data = ["r", "a", "c", "e", "c", "a", "r"]
11+
expected = ["r", "r", "a", "c", "e", "c", "a"]
12+
expected_head = "r"
13+
14+
for d in data:
15+
linked_list.append(d)
16+
17+
linked_list.move_tail_to_head()
18+
actual_head = linked_list.head
19+
20+
self.assertIsNotNone(actual_head)
21+
self.assertEqual(expected_head, actual_head.data)
22+
self.assertIsNone(actual_head.previous)
23+
24+
actual_data = []
25+
while actual_head:
26+
actual_data.append(actual_head.data)
27+
actual_head = actual_head.next
28+
29+
self.assertEqual(expected, actual_data)
30+
31+
def test_2(self):
32+
"""should move tail of ["a", "b", "c", "d"] to become ["d", "a", "b", "c"]"""
33+
linked_list = DoublyLinkedList()
34+
data = ["a", "b", "c", "d"]
35+
expected = ["d", "a", "b", "c"]
36+
expected_head = "d"
37+
38+
for d in data:
39+
linked_list.append(d)
40+
41+
linked_list.move_tail_to_head()
42+
actual_head = linked_list.head
43+
44+
self.assertIsNotNone(actual_head)
45+
self.assertEqual(expected_head, actual_head.data)
46+
self.assertIsNone(actual_head.previous)
47+
48+
actual_data = []
49+
while actual_head:
50+
actual_data.append(actual_head.data)
51+
actual_head = actual_head.next
52+
53+
self.assertEqual(expected, actual_data)
54+
55+
56+
if __name__ == "__main__":
57+
unittest.main()

datastructures/linked_lists/singly_linked_list/test_singly_linked_move_tail_to_head.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from . import SinglyLinkedList
44

55

6-
class LinkedListIsPalindromeTestCase(unittest.TestCase):
6+
class LinkedListMoveTailToHeadTestCase(unittest.TestCase):
77
def test_1(self):
88
"""should move tail of ["r", "a", "c", "e", "c", "a", "r"] to become ["r", "r", "a", "c", "e", "c", "a"]"""
99
linked_list = SinglyLinkedList()

0 commit comments

Comments
 (0)