diff --git a/reverse-linked-list/jylee2033.py b/reverse-linked-list/jylee2033.py new file mode 100644 index 000000000..2729d6e8a --- /dev/null +++ b/reverse-linked-list/jylee2033.py @@ -0,0 +1,27 @@ +# 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: Optional[ListNode]) -> Optional[ListNode]: + # Handle empty list + if head is None: + return head + + prev = None + cur = head # Start from the head node + + # Traverse until the last node + while cur.next is not None: + nxt = cur.next # Store next node + cur.next = prev # Reverse the link + prev = cur # Move prev forward + cur = nxt # Move cur forward + + # Handle the last node + cur.next = prev + return cur + +# Time Complexity: O(n) +# Space Complexity: O(1)