-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.py
More file actions
31 lines (28 loc) · 783 Bytes
/
Copy pathlinkedList.py
File metadata and controls
31 lines (28 loc) · 783 Bytes
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
class Node:
def __init__(self, value):
self.value = value
self.child = None
def addNode(self, value):
self.child = Node(value)
return self.child
def walkList(self):
print(self.value, " ")
if self.child is not None:
self.child.walkList()
def invertList(self):
previousNode = self
currentNode = previousNode.child
previousNode.child = None
while currentNode is not None:
nextNode = currentNode.child
currentNode.child = previousNode
previousNode = currentNode
currentNode = nextNode
head = Node(0)
tail = head.addNode(3)
tail = tail.addNode(11)
tail = tail.addNode(4)
head.walkList()
head.invertList()
print()
tail.walkList()