Reverse Linked List
Reverse a singly linked list. Tags: Linked List
Try It!
Discussion
Video
Solution
# 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: ListNode) -> ListNode:
first = head
second = None
while first:
temp = first.next
first.next = second
second = first
first = temp
return second