LeetCode: Reverse Linked List

Reverse a singly linked list.

Posted on LeetCode Python

Solution

Three-pointer technique with previous, current, and next.

While current is not None:

Runtime

0 ms | Beats 100.00%

class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
previous, current = None, head
while current:
next = current.next
current.next = previous
previous, current = current, next
return previous