-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist-reverse.py
113 lines (96 loc) · 2.96 KB
/
linkedlist-reverse.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class Node(object):
def __init__(self, data, next_node=None):
self.data = data
self.next_node = next_node
def get_next(self):
return self.next_node
def get_data(self):
return self.data
class LinkedList(object):
def __init__(self, head=None):
self.head = head
self.first = None
def insert(self, node):
if self.head:
self.head.next_node = node
else:
self.first = node
self.head = node
def size(self):
current = self.first
if not current:
return 0
nodes = 1
while current.next_node:
current = current.next_node
nodes += 1
return nodes
def search(self, value):
current = self.first
i = 0
while current:
if current.data == value:
return i
current = current.get_next()
i += 1
raise ValueError("{} not found in list".format(value))
def delete(self, value):
current = self.first
previous = None
while current:
if current.data == value:
if previous:
if current.get_next():
previous.next_node = current.get_next()
else:
previous.next_node = None
else:
if current.get_next():
self.first = current.get_next()
else:
self.first = None
return
else:
previous = current
current = current.get_next()
def reverse(self):
original_first = self.first
current = self.first
previous = None
i = 0
while current:
i += 1
if current == original_first:
# If we are on the first node
if not current.next_node:
# End early if not a 2nd node.
break
else:
previous = current
current = current.get_next()
continue
if i == 2: # 1 based instead of the usual 0
previous.next_node = None
original_next_node = current.get_next()
current.next_node = previous
if original_next_node:
previous = current
current = original_next_node
else:
self.first = current
current = None
if __name__ == "__main__":
LL = LinkedList()
LL.insert(Node("a"))
LL.insert(Node("b"))
# LL.insert(Node("c"))
pointer = LL.first
while pointer:
print "Current value: {}".format(pointer.data)
pointer = pointer.get_next()
LL.reverse()
print "Reversed!"
pointer = LL.first
while pointer:
print "Current value: {}".format(pointer.data)
pointer = pointer.get_next()