-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedlist.py
More file actions
41 lines (32 loc) · 932 Bytes
/
Linkedlist.py
File metadata and controls
41 lines (32 loc) · 932 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
#Rearrange Nodes of a Linked List such that elements at even position comes before odd ones
class Node :
def __init__(self, data) :
self.data = data
self.next = None
arr = [int(n) for n in input("Enter the elements of the Linked List\n").split(",")]
ls = Node(arr[0])
head = ls
ls = Node(arr[1])
head.next = ls
for i in range(2,len(arr)):
ls.next = Node(arr[i])
ls = ls.next
even = head
odd = head.next
odd_curr = odd
even_curr = even
while 1:
if not even_curr or not odd_curr or not odd_curr.next:
even_curr.next = odd
break
even_curr.next = odd_curr.next
even_curr = odd_curr.next
if not even_curr.next:
odd_curr.next = None
even_curr.next = odd_curr
break
odd_curr.next = even_curr.next
odd_curr = even_curr.next
while head:
print(head.data,end="->");
head = head.next