mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
Create reverse_linked_list_II.py
This commit is contained in:
parent
c004a953a9
commit
397402f144
26
linked_lists/reverse_linked_list_II.py
Normal file
26
linked_lists/reverse_linked_list_II.py
Normal 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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user