forked from jadid800/love_babbar_450_dsa_questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path139-reverse-Linked-list.py
60 lines (47 loc) · 1.2 KB
/
139-reverse-Linked-list.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
class Solution:
#Function to reverse a linked list.
def __init__(self):
self.head= Node
def reverseList(self, head):
self.head= head
prev= None
temp= head
next1= head.next
while(next1):
temp.next=prev
prev=temp
temp= next1
next1 =next1.next
temp.next= prev
return temp
class Node:
def __init__(self, val):
self.data = val
self.next = None
# Linked List Class
class Linked_List:
def __init__(self):
self.head = None
self.tail = None
def insert(self, val):
if self.head is None:
self.head = Node(val)
self.tail = self.head
else:
self.tail.next = Node(val)
self.tail = self.tail.next
def prinList(head):
temp= head
while(temp):
print(temp.data, end=" ")
temp= temp.next
print()
if __name__=='__main__':
for i in range(int(input())):
n = int(input())
arr = [int(x) for x in input().split()]
lis = Linked_List()
for i in arr:
lis.insert(i)
newHead = Solution().reverseList(lis.head)
prinList(newHead)