-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem7.py
More file actions
42 lines (34 loc) · 720 Bytes
/
problem7.py
File metadata and controls
42 lines (34 loc) · 720 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
31
32
33
34
35
36
37
38
39
40
41
42
# Reverse the elements of a singly linked list
class Node:
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def reverse(self):
prev = None
current = self.head
while current is not None:
next = current.next
current.next = prev
prev = current
current = next
self.head = prev
def push(self,new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def printList(self):
temp = self.head
while (temp):
print(temp.data)
temp = temp.next
l = LinkedList()
for i in range(5,0,-1):
l.push(i)
print(f"Given LinkedList:")
l.printList()
l.reverse()
print(f"Reversing it:")
l.printList()