Question #229903

There is a singly linked list represented by the following structure: struct Node int data; struct Node* next; Implement the following function: struct Node DeleteNodes (struct Node* head); The function accepts a pointer to the start of the linked list, 'head' argument. Delete all such nodes from the input list whose adjacent on the right side has greater value and return the modified linked list. Note: . Return null if the list is empty (Incase of python if the list is No return None). Do not create a new linked list, just modify the input linked list an . return it. Example: Input: head: 6->2->5->4->9->7->2>1>5->9 Output: 65-9->7->2->9 Explanation: Node '2' is deleted as '2' < '5' then '4' is deleted as '4' < '9' then '1' deleted as '1' < '5' then '5' is deleted as '5' < '9'. Sample Input head: 9- 5 - 6 - 2 -> 7 Sample Output​

Expert's answer

#Salve in Python
def DeleteNodes(ls):
    n=len(ls)
    for i in range(n):
        if i+1<len(ls) and ls[i]<ls[i+1]:
            ls.pop(i)
            n-=1
            i-=1
    if len(ls)>2:
        if ls[len(ls)-2]<ls[len(ls)-1]:
            ls.pop(len(ls)-2)
    return ls
ls=list(map(int,input().split(' ')))
ls=DeleteNodes(ls)
print(ls)
LATEST TUTORIALS
APPROVED BY CLIENTS