diff --git a/reverse-linked-list/doh6077.py b/reverse-linked-list/doh6077.py new file mode 100644 index 0000000000..569e995de2 --- /dev/null +++ b/reverse-linked-list/doh6077.py @@ -0,0 +1,19 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +# 206. Reverse Linked List +# 1. Use two pointers +# 2. one pointer indicates the current node and another pointer indicate the previous node +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + curr = head + prev = None + + while curr is not None: + nextNode = curr.next + curr.next = prev + prev = curr + curr = nextNode + return prev