Removing all NonLeader nodes from a singly Linked List. | gyanendraweblog
Non leader Node means any node to its right is larger than this node.
suppose initial list was: 1->7->3->5->4
Here 1 and 3 are non leader node since node right to it is larger than this.
So resultant list would be: 7->5->4
Steps to be followed
Since we have to traverse the list in reverse direction that is from last
so through recursion we can achieve the same.
1.) if this is the last node set max as data of this node and return this node
2.) Call the recursive function for next node.
3.) If value of this node it smaller than max, delete this node.
4.) Else if value of this is larger than max, update max as data of this node.
Read full article from Removing all NonLeader nodes from a singly Linked List. | gyanendraweblog
No comments:
Post a Comment