Interview task for linked list
Class Node wich has 2 attributes - value and next
value integer key
next - object of the Node class
Create find_delete_sort(node, key) which delete all nodes with value == key in the linked list started with node
- Trying to find first element in the linked list with
value != keyand set is as anew_headfor the returned list - Suppose that resultant list is already sorted. We take the next element in the original list, check if
value != keyand insert it in the necessary place in returned list. Ifvalue == keytake the next element in the original list - Insert function. Compare
valueof passed node with all the values of the nodes in resultant list. As soon as passed node value less or equal the checked node value, passed node is inserted. Function returns head of resultant list - Do this steps 2-3 until all elements in the orifinal list are checked.
Additional checks:
- If node was checked,
is_usedattribute is set to the node. I helps to avoid infinite loops for the looped linked list. Before returning new listis_usedattribute is deleted from all the nodes - Function arguments check. Input arguments should be instances of the
Nodeandintclasses - Node value check. If Node value is not
int, function stops due further it requires to compare Node values via logical operations - Added
__str__magic method for debugging purposes