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
25 changes: 25 additions & 0 deletions linkedlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def reverseList(self, head: [ListNode]) -> [ListNode]:

prev = None
curr = head

while curr:
# Store the next node
next_temp = curr.next

# Reverse the current node's pointer
curr.next = prev

# Move pointers one position ahead
prev = curr
curr = next_temp

# 'prev' is the new head
return prev