Skip to content

Commit bfb321e

Browse files
reverse passes
1 parent 117a71c commit bfb321e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

reverse/reverse.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
class Node:
2-
def __init__(self, value=None, next_node=None):
2+
def __init__(self, value=None, next=None):
33
self.value = value
4-
self.next_node = next_node
4+
self.next = next
55

66
def get_value(self):
77
return self.value
88

99
def get_next(self):
10-
return self.next_node
10+
return self.next
1111

1212
def set_next(self, new_next):
13-
self.next_node = new_next
13+
self.next = new_next
14+
1415

1516
class LinkedList:
1617
def __init__(self):
@@ -39,4 +40,20 @@ def contains(self, value):
3940
return False
4041

4142
def reverse_list(self, node, prev):
42-
pass
43+
if self.head == None:
44+
return None
45+
# prev as none
46+
prev = None
47+
# current as head
48+
current = self.head
49+
50+
while current != None:
51+
# store the next node
52+
next = current.next
53+
# reverse the pointer to prev
54+
current.next = prev
55+
# move prev and current forward
56+
prev = current
57+
current = next
58+
59+
self.head = prev

0 commit comments

Comments
 (0)