Skip to content
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
56 changes: 56 additions & 0 deletions Sprint-2/implement_linked_list/linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.previous = None

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

def push_head(self, data):
node = Node(data)

node.next = self.head
if self.head is None:
self.tail = node
else:
self.head.previous = node
self.head = node
return node

def remove(self, node):
if node is None:
return

if node.previous is None:
self.head = node.next
else:
node.previous.next = node.next

if node.next is None:
self.tail = node.previous
else:
node.next.previous = node.previous
node.previous = None
node.next = None

def pop_tail(self):
if self.head is None:
return
if self.tail is None:
return None
Comment on lines +40 to +43
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you simplify the code on lines 40-43?


node = self.tail
if node.previous is None:
self.head = None
self.tail = None
else:
self.tail = node.previous
self.tail.next = None
node.previous = None
Comment on lines +46 to +52
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also just call remove(node) -- less code to maintain.

node.next = None
Copy link

@cjyuan cjyuan Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the value of node.next before line 53?

return node.data