-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.py
More file actions
68 lines (54 loc) · 1.45 KB
/
LinkedList.py
File metadata and controls
68 lines (54 loc) · 1.45 KB
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
# Any data
# Sorted/Unsorted
# Not indexed
#Con:
# Slow to get kth element.
#Adv:
# insert and delete can be quick.
class Node:
def __init__(self,data):
self.next = None
self.data = data
class LinkedList:
def __init__(self):
self.head = None
def append(self,data):
if (self.head == None):
self.head = Node(data)
return
current = self.head
while (current.next != None):
current = current.next
current.next = Node(data)
def prepend(self,data):
newHead = Node(data)
newHead.next = self.head
self.head = newHead
def deleteWithValue(self,data):
if (self.head == None):
return
if (head.data == data):
head = head.next
return
current = self.head
while (current.next != None): #Continue walking through the linked list.
if (current.next.data == data):
current.next = current.next.next
return
current = current.next
def printList(self):
current = self.head
while (current.next !=None):
print current.data
current = current.next
print current.data
def main():
a = LinkedList()
a.append(1)
a.append(2)
a.append(5)
a.append(10)
a.prepend(0)
a.printList()
if __name__== "__main__":
main()