public void setPrevious(LinkListNode p) { prev = p; if (p != null && p.next != this) { p.setNext(this); } }
//method 1: copy next node to n, delete next node, O(1) time, O(1) space static bool Delete(LinkListNode n) { if (n.next == null || n == null) return false; else { n.data = n.next.data; n.next = n.next.next; return true; } }
public LinkListNode clone() { LinkListNode next2 = null; if (next != null) { next2 = next.clone(); } LinkListNode head2 = new LinkListNode(data, next2, null); return head2; }
public LinkListNode clone() { LinkListNode next2 = null; if (next != null) { next2 = next.clone(); } LinkListNode head2 = new LinkListNode(data, next2, null); return(head2); }
public void setNext(LinkListNode n) { next = n; if (this == last) { last = n; } if (n != null && n.prev != this) { n.setPrevious(this); } }
//method 1: copy next node to n, delete next node, O(1) time, O(1) space static bool Delete(LinkListNode n) { if (n.next == null || n == null) { return(false); } else { n.data = n.next.data; n.next = n.next.next; return(true); } }
//Delete middle node: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node. static void Main(string[] args) { LinkListNode head = new LinkListNode(0); LinkListNode first = head; for(int i = 1; i < 10; i++){ LinkListNode second = new LinkListNode(i); first.setNext(second); first = second; } Console.WriteLine(head.printForward()); Delete(head.next.next.next.next.next); Console.WriteLine(head.printForward()); }
{ //Delete middle node: Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node. static void Main(string[] args) { LinkListNode head = new LinkListNode(0); LinkListNode first = head; for (int i = 1; i < 10; i++) { LinkListNode second = new LinkListNode(i); first.setNext(second); first = second; } Console.WriteLine(head.printForward()); Delete(head.next.next.next.next.next); Console.WriteLine(head.printForward()); }
public LinkListNode(int d, LinkListNode n, LinkListNode p) { data = d; setNext(n); setPrevious(p); }