Question: You are given a single linked list of integer values. Write a function to re-arrange the list such that all odd values should be removed
- You are given a single linked list of integer values. Write a function to re-arrange the list such that all odd values should be removed the list without affecting the order of the list. You can use the following classes for your reference. You can add any other functions if required. Your function will be tested using ToList() so make sure that your solution consist this function which is already given.
class Node:
def __init__(self,value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def InsertAtFirst(self, e):
n = Node(e)
n.next = self.head
self.head = n
def ToList(self):
num = []
x = self.head
while (x != None):
num.append(x.value)
x = x.next
return(num)
def ReArrange(self):
// your code goes here
Example:
l = LinkedList()
l.InsertAtFirst(5)
l.InsertAtFirst(4)
l.InsertAtFirst(10)
l.InsertAtFirst(3)
l.ReArrange()
print(l.ToList())
# output
[10, 4]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
