-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverse_linked_list.py
More file actions
58 lines (44 loc) · 1.02 KB
/
reverse_linked_list.py
File metadata and controls
58 lines (44 loc) · 1.02 KB
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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert(head, value):
node = Node(value)
if head is None:
head = node
return head
temp = head
while temp.next:
temp = temp.next
temp.next = node
return head
llist1 = LinkedList()
llist1.head = insert(llist1.head, 1)
llist1.head = insert(llist1.head, 2)
llist1.head = insert(llist1.head, 3)
llist1.head = insert(llist1.head, 4)
#llist1.head = insert(llist1.head, 5)
#llist1.head = insert(llist1.head, 6)
def view(head):
temp = head
temp2 = temp
while temp:
print(temp.data)
temp = temp.next
def reverse(llist):
temp = llist
if temp == None:
print("No element")
prev = None
while temp:
next = temp.next
temp.next = prev
prev = temp
temp = next
answer = LinkedList()
answer.head = prev
view(answer.head)
reverse(llist1.head)