-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
79 lines (65 loc) · 1.87 KB
/
linked_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class SListNode:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
def __repr__(self):
return repr(self.data)
class SinglyLinkedList:
def __init__(self):
self.head = None
def prepend(self, data):
self.head = SListNode(data=data, next=self.head)
def append(self, data):
if not self.head:
self.head = SListNode(data=data)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = SListNode(data=data)
def remove(self, key):
curr = self.head
prev = None
while curr and curr.data != key:
prev = curr
curr = curr.next
if prev is None:
self.head = curr.next
elif curr:
prev.next = curr.next
curr.next = None
class DListNode:
def __init__(self, data=None, prev=None, next=None):
self.data = data
self.prev = prev
self.next = next
def __repr__(self):
return repr(self.data)
class DoublyLinkedList:
def __init__(self):
self.head = None
def prepend(self, data):
new_head = DListNode(data=data, next=self.head)
if self.head:
self.head.prev = new_head
self.head = new_head
def append(self, data):
if not self.head:
self.head = DListNode(data=data)
return
curr = self.head
while curr.next:
curr = curr.next
curr.next = DListNode(data=data, prev=curr)
def remove(self, key):
elem = self.find(key)
if not elem:
return
if elem.prev:
elem.prev.next = elem.next
if elem.next:
elem.next.prev = elem.prev
if elem is self.head:
self.head = elem.next
elem.prev = None
elem.next = None