mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
Create reverse_linked_list_II.py
This commit is contained in:
parent
c004a953a9
commit
397402f144
1 changed files with 26 additions and 0 deletions
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…
Add table
Add a link
Reference in a new issue