public void setPrevious(LinkListNode p) { prev = p; if (p != null && p.next != this) { p.setNext(this); } }
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); } }
static void removeDupe(LinkListNode head) { LinkListNode n = head; LinkListNode prev = null; HashSet <int> checker = new HashSet <int>(); while (n != null) { if (checker.Contains(n.data)) { prev.next = n.next; } else { checker.Add(n.data); prev = n; } n = n.next; } }
static void removeDupe(LinkListNode head) { LinkListNode n = head; LinkListNode prev = null; HashSet<int> checker = new HashSet<int>(); while (n != null) { if (checker.Contains(n.data)) { prev.next = n.next; } else { checker.Add(n.data); prev = n; } n = n.next; } }
static void removeDupe2(LinkListNode head) { LinkListNode p1 = head; LinkListNode p2; while (p1 != null) { p2 = p1; while (p2.next != null) { if (p2.next.data == p1.data) { p2.next = p2.next.next; } else { p2 = p2.next; } } p1 = p1.next; } }
//Write code to remove duplicates from an unsorted link. Follow up: how would you solve this problem if a temp buffer is not allowed? static void Main(string[] args) { LinkListNode first = new LinkListNode(0, null, null); LinkListNode head = first; LinkListNode second = first; for (int i = 1; i < 8; i++) { second = new LinkListNode(i % 2, null, null); first.setNext(second); second.setPrevious(first); first = second; } LinkListNode cloneA = head.clone(); LinkListNode cloneB = head.clone(); Console.WriteLine(cloneA.printForward()); removeDupe(cloneA); Console.WriteLine(cloneA.printForward()); Console.WriteLine(cloneB.printForward()); removeDupe2(cloneB); Console.WriteLine(cloneB.printForward()); }
{//Write code to remove duplicates from an unsorted link. Follow up: how would you solve this problem if a temp buffer is not allowed? static void Main(string[] args) { LinkListNode first = new LinkListNode(0, null, null); LinkListNode head = first; LinkListNode second = first; for (int i = 1; i < 8; i++) { second = new LinkListNode(i % 2, null, null); first.setNext(second); second.setPrevious(first); first = second; } LinkListNode cloneA = head.clone(); LinkListNode cloneB = head.clone(); Console.WriteLine(cloneA.printForward()); removeDupe(cloneA); Console.WriteLine(cloneA.printForward()); Console.WriteLine(cloneB.printForward()); removeDupe2(cloneB); Console.WriteLine(cloneB.printForward()); }
public LinkListNode(int d, LinkListNode n, LinkListNode p) { data = d; setNext(n); setPrevious(p); }
static void removeDupe2(LinkListNode head) { LinkListNode p1 = head; LinkListNode p2; while (p1 != null) { p2 = p1; while (p2.next != null) { if (p2.next.data == p1.data) { p2.next = p2.next.next; } else p2 = p2.next; } p1 = p1.next; } }