Reverse Linked List
- python
- linked lists
Problem URL:Reverse Linked List
My Solution
Python
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseList(head: Optional[ListNode]) -> Optional[ListNode]:
curr = head
stack = []
reversed_list = None
while curr:
reversed_list = ListNode(curr.val, reversed_list)
stack.append(reversed_list)
curr = curr.next
return reversed_list
Let's Connect
Twitter •GitHub •LinkedIn