Skip to main content

Delete nodes which have a greater value on right side

Given a linked list, Which include integer node values. Our goal is to remove all nodes which value is less than on right side node. This problem is based on linked list traversal and node deletion with specified condition. Lets see few examples to understand this problem.

Example
----------
    List    1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
        -------------------------------------------
            1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
            1 < 2 (Yes) Delete node 1

            2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
            2 < 5 (Yes) Delete node 2

            5 → 4 → 3 → 6 → 7 → 8 → NULL
            5 < 4 (No)   

            5 → 4 → 3 → 6 → 7 → 8 → NULL
                4 < 3 (No)  

            5 → 4 → 3 → 6 → 7 → 8 → NULL
                    3 < 6 (Yes) Delete node 3  
          
            5 → 4 → 6 → 7 → 8 → NULL   
                    6 < 7 (Yes)

            5 → 4 → 7 → 8 → NULL   
                    7 < 8 (Yes) Delete node 7

            5 → 4 → 8 → NULL
        -------------------------------------------
Output :    5 → 4 → 8 → NULL

Here given code implementation process.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment