We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e75cdd4 commit dde78d8Copy full SHA for dde78d8
1 file changed
reverse-linked-list/jylee2033.py
@@ -6,22 +6,22 @@
6
class Solution:
7
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
8
# Handle empty list
9
- if head is None:
10
- return head
+ # if head is None:
+ # return head
11
12
prev = None
13
cur = head # Start from the head node
14
15
# Traverse until the last node
16
- while cur.next is not None:
+ while cur is not None:
17
nxt = cur.next # Store next node
18
cur.next = prev # Reverse the link
19
prev = cur # Move prev forward
20
cur = nxt # Move cur forward
21
22
# Handle the last node
23
- cur.next = prev
24
- return cur
+ # cur.next = prev
+ return prev
25
26
# Time Complexity: O(n)
27
# Space Complexity: O(1)
0 commit comments