mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
Create reverse_linked_list.py
This commit is contained in:
parent
40034ce8bd
commit
614f985f1a
20
linked_lists/reverse_linked_list.py
Normal file
20
linked_lists/reverse_linked_list.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
class ListNode:
|
||||||
|
def __init__(self, val=0, next=None):
|
||||||
|
self.val = val
|
||||||
|
self.next = next
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_list(head: Optional[ListNode]) -> Optional[ListNode]:
|
||||||
|
|
||||||
|
if (not head) or (not head.next):
|
||||||
|
return head
|
||||||
|
|
||||||
|
new_head = reverse_list(head.next)
|
||||||
|
head.next.next = head
|
||||||
|
head.next = None
|
||||||
|
|
||||||
|
return new_head
|
Loading…
x
Reference in New Issue
Block a user