-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.py
More file actions
46 lines (44 loc) · 898 Bytes
/
Copy pathLinkedList.py
File metadata and controls
46 lines (44 loc) · 898 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
45
46
# Linked List
class Node:
def __init__(self, data = None):
self.next = None
self.data = data
class LinkedList:
def __init__(self):
self.head = Node() # wrap the Node class
def push(self, data):
new_node = Node(data)
cur = self.head
while cur.next:
cur = cur.next
cur.next = new_node
def isEmpty(self):
return self.head.next is None
def pop(self):
if self.isEmpty():
return None
cur = self.head
while cur.next.next:
cur = cur.next
pop = cur.next.data
cur.next = None
return pop
def display(self):
elements = []
cur = self.head
while cur.next:
cur = cur.next
elements.append(cur.data)
return elements
ll = LinkedList()
print('Is Empty', ll.isEmpty())
print('Pop', ll.pop())
ll.push(1)
ll.push(2)
ll.push(3)
ll.push(4)
ll.push(5)
print('Is Empty', ll.isEmpty())
print('Print', ll.display())
print('Pop', ll.pop())
print('Print', ll.display())