-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.py
63 lines (51 loc) · 1.41 KB
/
LinkedList.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
class Node:
def __init__(self,val):
self.val = val
self.next = None
class LinkedList:
def __init__(self, head):
self.head = Node(head.val)
def display(self):
current = self.head
while current is not None:
print(current.val)
current = current.next
def insert(self,value):
current = self.head
while current.next is not None:
current = current.next
current.next = Node(value)
def insertToHead(self,value):
newHead = Node(value)
newHead.next=self.head
self.head = newHead
def size(self):
len = 0
if self.head is None:
return len
else:
current = self.head
while current.next is not None:
current = current.next
len += 1
return len + 1
def deleteHead(self):
self.head = self.head.next
def search(self,value):
i=0
current = self.head
while current.val is not value and current.next is not None:
i += 1
current = current.next
return -1
return current.val, i
node1 = Node("hamzawey")
l = LinkedList(node1)
l.insert("hamza")
l.insertToHead("newHead")
l.display()
print(l.size())
l.deleteHead()
l.display()
print(l.size())
print (l.search("s"))