Skip to content

Commit 3c00072

Browse files
committed
search data in linked list
1 parent cc6d2b0 commit 3c00072

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

search.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Node:
2+
def __init__(self, data):
3+
self.data = data
4+
self.next = None
5+
6+
7+
class LinkedList:
8+
def __init__(self):
9+
self.head = None
10+
11+
def search(self, value):
12+
temp = self.head
13+
while temp is not None:
14+
if temp.data == value:
15+
return True
16+
17+
temp = temp.next
18+
return False
19+
20+
def printList(self):
21+
temp = self.head
22+
while temp is not None:
23+
print(temp.data)
24+
temp = temp.next
25+
26+
27+
l1 = LinkedList()
28+
l1.head = Node(10)
29+
l2 = Node(20)
30+
l3 = Node(30)
31+
l4 = Node(40)
32+
l1.head.next = l2
33+
l2.next = l3
34+
l3.next = l4
35+
l1.printList()
36+
if l1.search(20):
37+
print("YES")
38+
else:
39+
print("NO")

0 commit comments

Comments
 (0)