Skip to content

Commit dde78d8

Browse files
committed
update reverse linked list
1 parent e75cdd4 commit dde78d8

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

reverse-linked-list/jylee2033.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
class Solution:
77
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
88
# Handle empty list
9-
if head is None:
10-
return head
9+
# if head is None:
10+
# return head
1111

1212
prev = None
1313
cur = head # Start from the head node
1414

1515
# Traverse until the last node
16-
while cur.next is not None:
16+
while cur is not None:
1717
nxt = cur.next # Store next node
1818
cur.next = prev # Reverse the link
1919
prev = cur # Move prev forward
2020
cur = nxt # Move cur forward
2121

2222
# Handle the last node
23-
cur.next = prev
24-
return cur
23+
# cur.next = prev
24+
return prev
2525

2626
# Time Complexity: O(n)
2727
# Space Complexity: O(1)

0 commit comments

Comments
 (0)