-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.py
More file actions
44 lines (37 loc) · 810 Bytes
/
Copy pathlinkedlist.py
File metadata and controls
44 lines (37 loc) · 810 Bytes
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
from node import Node
class LinkedList:
def __init__(self, array=None):
self.first = None
self.last = None
if array is not None and len(array) > 0:
for c in array:
self.insert(c)
def insert(self, n):
node = Node(n)
if self.first is None:
self.first = node
self.last = node
else:
self.last.next = node
self.last = self.last.next
def append(self, node):
if self.first is None:
print(node)
self.first = node
current = node
while current.next is not None:
current = current.next
self.last = current
else:
self.last.next = node
def __str__(self):
if self.first is not None:
current = self.first
s = '['
while current is not None:
s += str(current.data) + ' '
current = current.next
s += ']'
return s
else:
return '[]'