-
Notifications
You must be signed in to change notification settings - Fork 0
/
206_ReverseLinkedList.py
83 lines (64 loc) · 2.39 KB
/
206_ReverseLinkedList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# By Will Shin
#
#-------------------------------------------------------------------------------
# LeetCode prompt
#-------------------------------------------------------------------------------
"""
"""
# #
#-------------------------------------------------------------------------------
# Approach
#-------------------------------------------------------------------------------
"""
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
"""
#-------------------------------------------------------------------------------
# Solution
#-------------------------------------------------------------------------------
def my_reverseList_helper(previous_node, current_node):
if current_node.next == None:
current_node.next = previous_node
return current_node
next_node = current_node.next
current_node.next = previous_node
return my_reverseList_helper(current_node, next_node)
def my_reverseList(head):
# return if empty
if head is None:
return
# recursion begins
new_head = my_reverseList_helper(None, head)
return(new_head)
#-------------------------------------------------------------------------------
# Main Leetcode Input Driver
#-------------------------------------------------------------------------------
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverseList(self, head):
return my_reverseList(head)
#-------------------------------------------------------------------------------
# Unit Test
#-------------------------------------------------------------------------------
import unittest
from mymodules import LinkedLists as linked
class TestSolution(unittest.TestCase):
def test_simple(self):
my_ll = linked.LinkedList()
my_ll.addList([1,2,3,4,5])
reversed_list = [5, 4, 3, 2, 1]
returned = Solution().reverseList(my_ll.head)
returned_ll = linked.LinkedList(head = returned)
self.assertEqual(returned_ll.printList(), reversed_list)
unittest.main()