π―
About the Chipotle orderβ¦
I can't actually place a real Chipotle order β I don't have access to their ordering system, your payment info, or a delivery address. You'd want to use the Chipotle app, chipotle.com, or DoorDash/Uber Eats for that.
But I can help you build the perfect order to paste in β and definitely reverse that linked list. π
π₯£ Burrito Bowl Builder (so you know exactly what to tap in the app)
This is just a mockup β no order gets placed. On to the real work. π
π Reversing a Python Linked List
Here's the classic iterative solution β the one that shows up in every interview. Watch it run step-by-step below.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse(head):
prev = None
curr = head
while curr:
nxt = curr.next # 1. remember the rest of the list
curr.next = prev # 2. flip the current pointer
prev = curr # 3. advance prev
curr = nxt # 4. advance curr
return prev # new head
Click Step to walk through the algorithm.
Complexity: O(n) time, O(1) space. There's also a slick recursive version if you like that flavor.