Create reverse_linked_list_II.py

This commit is contained in:
marina 2023-08-02 18:13:43 -07:00 committed by GitHub
parent c004a953a9
commit 397402f144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class Node:
def __init__(self, val=0, next):
self.val = val
self.next = next
def reverse_list(head: Optional[Node]) -> Optional[Node]:
if head is None:
return head
final_head = head
while head.next:
new_node = head.next
head.next = new_node.next
new_node.next = final_head
final_head = new_node
return final_head