-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.py
More file actions
66 lines (55 loc) · 2.28 KB
/
Copy pathLinkedList.py
File metadata and controls
66 lines (55 loc) · 2.28 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
# In Python we deal with data structures like lists,
# but we don't talk much about how lists are implemented
# by Python itself.
#
# One way in which lists can be implemented is with what
# is called a Linked List. In a Linked List, a list is
# made of a series of items. Each item contains two
# elements: its own value (node.value), and a pointer to
# where to find the next element in the list
# (node.next_node). So, you can't jump right in to
# "item 7"; you instead have to start with the first
# item and keep getting the next item until you reach the
# seventh item.
#
# Below is some code we've written to implement a
# Linked List. Specifically, this code represents a
# single node in a linked list: a full list is created
# just by chaining nodes like these together. The last
# node in the linked list will point to None as the
# next item; that indicates you've reached the end of
# the linked list.
#
# Write a function called linked_list_search. Your
# linked_list_search function should take two parameters:
# a single linked list node, and a search term. Your
# function should return True if the search term is the
# value of the linked list node or any node after it.
# It should return False if the value is not found in
# the linked list.
class LinkedListNode:
def __init__(self, value, next_node=None):
self.value = value
self.next_node = next_node
def linked_list_search(root_node, value: int) -> bool:
"""Returns True if value in linked list, otherwise False."""
current_node = root_node
while current_node is not None:
if current_node.value == value:
return True
current_node = current_node.next_node
return False
# Write your function here!
# Below are lines of code that create a linked list,
# and then search for two values in that linked list:
# one that is there, one that isn't. If your function
# works, it should print True then False.
node_7 = LinkedListNode(5)
node_6 = LinkedListNode(2, node_7)
node_5 = LinkedListNode(9, node_6)
node_4 = LinkedListNode(1, node_5)
node_3 = LinkedListNode(4, node_4)
node_2 = LinkedListNode(6, node_3)
root_node = LinkedListNode(7, node_2)
print(linked_list_search(root_node, 9))
print(linked_list_search(root_node, 3))