Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linked list code in python #351

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions python/Linked_List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None


class LinkedList:
def __init__(self):
self.head = None
self.size = 0

def __str__(self):
nodes = []
current = self.head
while current:
nodes.append(str(current.value))
current = current.next
return ' -> '.join(nodes)

def __len__(self):
return self.size

def insert_at_head(self, value):
new_node = Node(value)
new_node.next = self.head
self.head = new_node
self.size += 1

def insert_at_tail(self, value):

if not self.head:
self.head = Node(value)
else:
current = self.head
while current.next:
current = current.next
current.next = Node(value)
self.size += 1

def delete_at_head(self):
if not self.head:
return None
value = self.head.value
self.head = self.head.next
self.size -= 1
return value

def delete(self, value):
if not self.head:
return None
if self.head.value == value:
return self.delete_at_head()
current = self.head
while current.next:
if current.next.value == value:
value = current.next.value
current.next = current.next.next
self.size -= 1
return value
current = current.next
return None

def find(self, value):
current = self.head
while current:
if current.value == value:
return current
current = current.next
return None

linked_list = LinkedList()
linked_list.insert_at_head(1)
linked_list.insert_at_head(2)
linked_list.insert_at_head(3)
print(linked_list)
print(len(linked_list))

linked_list.insert_at_tail(4)
print(linked_list)
print(len(linked_list))

deleted_value = linked_list.delete_at_head()
print(deleted_value)
print(linked_list)
print(len(linked_list))

found_node = linked_list.find(2)
print(found_node.value)