Example #1
0
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   LList test;
   test = new LList();
   test.append("vikas");
   test.append(1);
   test.append(100);
   test.print();
   System.out.println(test);
 }
  /*
   * Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node
   * EXAMPLE
   * 		Input: the node ÔcÕ from the linked list a->b->c->d->e
   * 		Result: nothing is returned, but the new linked list looks like a->b->d->e
   */
  public void deleteNode(LList list, LList.Node c) {

    if ((null == list) || (null == c)) return;

    LList.Node n = list.head;

    if (n == c) {
      System.out.println("1:");
      list.head = list.head.next;
      return;
    }

    LList.Node nP = list.head;
    n = n.next;

    while (null != n) {
      System.out.println("2:");
      if (n.data == c.data) {
        System.out.println("3:");
        nP.next = c.next; // VB, it should be nP.next = n.next
        c.data = null; // n.data = null
        c.next = null; // n.next = null
        return;
      }
      nP = n;
      n = n.next;
    }
  }
  public static void main(String[] args) {
    LinkedListCareerCup l = new LinkedListCareerCup();
    /*
    java.util.LinkedList<Integer> list = new java.util.LinkedList<Integer>();
    	list.add(5);
    	list.add(25);
    	list.add(5);
    	list.add(35);

    System.out.println(l.getNthLastElement(list, 4));

    System.out.println("list:" + list.toString());
    System.out.println("list became:" + l.removeDuplicates(list));

    list = new LinkedList<Integer>();
    	list.add(5);
    	list.add(25);
    	list.add(5);
    	list.add(35);

    System.out.println("list:" + list.toString());
    System.out.println("list became:" + l.removeDuplicates1(list));

    // --------------------------
    LList ls = l.new LList();
    ls.insert(1);
    ls.insert(2);
    ls.insert(3);
    ls.insert(4);
    ls.insert(5);

    //System.out.println(ls);
    LList.Node n = ls.head.next.next.next.next.next;
    //System.out.println("@@@ " + n);
    l.deleteNode(ls, n);
    System.out.println(ls);
    // --------------------------

    LinkedList<Integer> list1 = new LinkedList<Integer>();
    list1.add(3);
    list1.add(1);
    list1.add(9);

    LinkedList<Integer> list2 = new LinkedList<Integer>();
    list2.add(5);
    list2.add(9);
    list2.add(2);

    System.out.println(list1);
    System.out.println(list2);
    System.out.println(l.addLists(list1, list2));
    // --------------------------
    */

    LList ls = l.new LList();
    ls.insert(1);
    ls.insert(2);
    ls.insert(3);
    ls.insert(4);
    ls.insert(5);

    System.out.println(ls);

    ls.tail.next = ls.head.next.next.next;
    System.out.println(l.getLoopBegining(ls));
    // System.out.println(ls);
  }