-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsll.py
92 lines (75 loc) · 2.31 KB
/
sll.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
80
81
82
83
84
85
86
87
88
89
90
91
92
# Singly Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def listLength(self):
currentNode = self.head
length = 0
while currentNode is not None:
length += 1
currentNode = currentNode.next
return length
def insertHead(self, newNode):
temporaryNode = self.head
self.head = newNode
self.head.next = temporaryNode
del temporaryNode
def insertAt(self, newNode, position):
if position < 0 or position > self.listLength():
print("Invalid Position")
return
if position == 0:
self.insertHead(newNode)
return
currentNode = self.head
currentPosition = 0
while True:
if currentPosition == position:
previousNode.next = newNode
newNode.next = currentNode
break
previousNode = currentNode
currentNode = currentNode.next
currentPosition += 1
def insertEnd(self, newNode):
if self.head is None:
self.head = newNode
else:
lastNode = self.head
while True:
if lastNode.next is None:
break
lastNode = lastNode.next
lastNode.next = newNode
def deleteEnd(self):
lastNode = self.head
while lastNode.next is not None:
previousNode = lastNode
lastNode = lastNode.next
previousNode.next = None
del lastNode
def printList(self):
if self.head is None:
print("List is empty!")
return
currentNode = self.head
while True:
if currentNode is None:
print(None)
break
print(currentNode.data, end=" -> ")
currentNode = currentNode.next
firstNode = Node(10)
secondNode = Node(20)
thirdNode = Node(15)
linkedList = LinkedList()
linkedList.insertEnd(firstNode)
linkedList.insertEnd(secondNode)
# linkedList.insertHead(thirdNode)
linkedList.insertAt(thirdNode, 1)
linkedList.deleteEnd() # 10 -> 15 -> None
linkedList.printList() # John -> Ben -> Kris -> None