c# - Singly Linked List. RemoveBefore(Node node) method -


i trying implement singly linked list. optimal way of implementing removebefore(listnode) method ? have done below.

  1. check if node being removed head. if return.
  2. check if node there 2 nodes , node being removed tail. set head , tail same. old tail automatically disposed.
  3. if there mode 2 nodes , above 2 conditions not satisfied track current node, child node , grandchild node. if grandchild node requested node make current node point grandchild node. child node disposed.

    void isinglylinkedlist.removebefore(isinglylinkednode node) {

        //cannot remove before head     if (object.equals(node.data, _head.data))         return;      //if need remove node before tail make head , tail same     if (object.equals(node.data, _tail.data) && _head.next == _tail )     {         _head = _tail;         (_tail idisposable).dispose();     }      (isinglylinkednode<t> currentnode = _head, child = null, grandchild = null; currentnode != null;  currentnode = currentnode.next)     {         child = currentnode.next;         grandchild = child.next;          if (object.equals(node.data, grandchild.data))         {             currentnode.next = grandchild;             break;         }      } } 

is above logic correct , optimal please ?


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -