-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverseLinkedList.py
32 lines (26 loc) · 985 Bytes
/
reverseLinkedList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
"""
Reverses a singly linked list using recursion.
Parameters:
head (Optional[ListNode]): The head of the singly linked list.
Returns:
Optional[ListNode]: The new head of the reversed linked list.
The function traverses to the end of the list recursively, then reverses the links
as the recursion unwinds, ensuring that all nodes point to their previous node
in the original list. The new head of the reversed list is returned.
"""
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
return None
elif not head.next:
return head
else:
new_head = self.reverseList(head.next)
head.next.next = head
head.next = None
return new_head