Question

In: Computer Science

Python Add your own implementation of the function rearrange_list rearrange_list(linkedlist, number) returns the result of rearranging...

Python Add your own implementation of the function rearrange_list

rearrange_list(linkedlist, number) returns the result of rearranging the nodes in linkedlist so that:

  • all the nodes whose values are less than number are at the front
  • all the nodes whose values are greater than number are at the rear
  • any nodes whose values are equal to number are in the middle


For example, given the linked list represented by

linkedlist = -2 -> -3 -> -1 -> 2 -> 3 -> 0 -> 4 -> 1 -> 5

then

rearrange_list(linkedlist, 2) == -2 -> -3 -> -1 -> 0 -> 1 -> 2 -> 3 -> 4 -> 5

You can see that all the numbers less than 2 now come before it, and all the numbers greater than 2 come after it. You can also see that the numbers maintain their relative position to each other, i.e. -2 and -3 and -1 don't change position relative to each other. This is an important characteristic, known as "stability". It also means that instead of requiring a full sort, you should be able to implement this algorithm in O(N) time.

You are given a simple Node class. Add your own implementation of the function rearrange_list (defined at the top level, not as part of the Node class)

Solutions

Expert Solution

ANSWER:

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):

if(temp.next!=None):

print(temp.data,end="->")

else:

print(temp.data,end="")

temp = temp.next

def populateList(self, number):

frontList=LinkedList()

rearList=LinkedList()

finalList=LinkedList()

found=False

temp = self.head

while(temp):

if temp.data==number :

found=True

else :

if temp.data<number :

frontList.push(temp.data)

else :

rearList.push(temp.data)

temp = temp.next

frontList.reverse()

rearList.reverse()

frontList.printList()

if found==True :

print("->"+str(number)+"->",end="")

rearList.printList()

num=input("Enter number of elements: ")

llist = LinkedList()

for x in range(int(num)):

val=input("Enter value: ")

llist.push(int(val))

llist.reverse()

print("\nGiven Linked List: ")

llist.printList()

print("\n\nEnter number to compare: ")

number=int(input())

print("\nFinal Result: ")

llist.populateList(number)

#SAMPLE OUTPUT

If you do not get anything in this solution,please put a comment and i will help you out.

Do not give a downvote instantly.It is a humble request.

If you like my answer,please give an upvote....Thank you.


Related Solutions

(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy...
• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy node”) that connects the end of the list with the beginning, transforming the list to a circular list Code in c++ The Josephus problem is named after the historian Flavius Josephus, who lived between the years 37 and 100 CE. Josephus was a reluctant leader of the Jewish revolt against the Roman Empire. When it appeared that Josephus and his band were to be...
Write python 3 code to define a function that uses three arguments, and returns a result....
Write python 3 code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False. Write a python 3 code for function main, that does the following: creates a variable and assign it the value True. uses a while loop which runs as long as the variable of the...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of...
Python: Implement edit_diff using recursion, which is a diff function that returns the minimum number of edit operations needed to transform the start word into the goal word. There are three kinds of edit operations: Add a letter to start, Remove a letter from start, Substitute a letter in start for another. Each edit operation contributes 1 to the difference between two words. >>> big_limit = 10 >>> edit_diff("roses", "arose", big_limit) # roses -> aroses -> arose 2 >>> edit_diff("tesng",...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT