예제 #1
0
  // return the head of the new linked list
  public static LinkedListNode partitionAround(LinkedListNode ll, int x) {
    // start from a list with only one node
    // we are actually creating a new linked list
    LinkedListNode head = ll;
    LinkedListNode tail = ll;

    while (ll != null) {
      // because the current node ll will be moved, we have to
      // save the next node for the loop to continue
      LinkedListNode next = ll.next;
      if (ll.data < x) {
        // prepend the node
        ll.next = head;
        // update the head
        head = ll;
      } else {
        // append the node
        tail.next = ll;
        // update the tail
        tail = ll;
      }
      ll = next;
    }
    tail.next = null;
    return head;
  }
예제 #2
0
 public LinkedListNode partition(LinkedListNode head, int x) {
   LinkedListNode ll = null, l = null, r = null, rr = null;
   while (head != null) {
     LinkedListNode next = head.next;
     head.next = null;
     if (head.data < x) {
       if (ll == null) {
         ll = head;
         l = ll;
       } else {
         l.next = head;
         l = head;
       }
     } else {
       if (r == null) {
         r = head;
         rr = r;
       } else {
         rr.next = head;
         rr = head;
       }
     }
     head = next;
   }
   if (ll == null) return l;
   l.next = r;
   return ll;
 }
예제 #3
0
 public static void main(String[] args) {
   LinkedListNode node1 = new LinkedListNode(10);
   LinkedListNode node2 = new LinkedListNode(15);
   LinkedListNode node3 = new LinkedListNode(12);
   node1.next = node2;
   node2.next = node3;
   System.out.println(getNthFromLast(1, node1));
 }
예제 #4
0
 public static void main(String[] args) {
   LinkedListNode node1 = new LinkedListNode(10);
   LinkedListNode node2 = new LinkedListNode(15);
   LinkedListNode node3 = new LinkedListNode(12);
   node1.next = node2;
   node2.next = node3;
   printList(node1);
   deleteFastNode(node1);
   printList(node1);
 }
예제 #5
0
 public void insert(int[] array, int startIndex) {
   for (int i = startIndex; i < array.length; i++) {
     LinkedListNode node = new LinkedListNode(array[i]);
     tail.next = node;
     tail = node;
   }
 }
예제 #6
0
 public static void main(String[] args) {
   LinkedListNode h1 = new LinkedListNode(1);
   LinkedListNode h2 = new LinkedListNode(5);
   LinkedListNode h3 = new LinkedListNode(3);
   LinkedListNode h4 = new LinkedListNode(9);
   LinkedListNode h5 = new LinkedListNode(2);
   h1.next = h2;
   h2.next = h3;
   h3.next = h4;
   h4.next = h5;
   LinkedListNode res = new CC204().partition1(h1, 4);
   while (res != null) {
     System.out.println(res.data);
     res = res.next;
   }
 }
예제 #7
0
 /**
  * Adds a node to the beginning of the list.
  *
  * @param node the node to add to the beginning of the list.
  */
 public LinkedListNode addFirst(LinkedListNode node) {
   node.next = head.next;
   node.previous = head;
   node.previous.next = node;
   node.next.previous = node;
   return node;
 }
예제 #8
0
 public static LinkedListNode buildList(int[] A) {
   LinkedListNode head = new LinkedListNode(A[0]);
   for (i = 1; i < A.length; i++) {
     head.next = new LinkedListNode(A[i]);
     head = head.next;
   }
   return head;
 }
예제 #9
0
  @Override
  public String toString() {
    LinkedListNode node = head;
    String res = new String("[");

    while (node != null) {
      res = res + node.value();
      if (node.next() != null) {
        res = res + ", ";
      }
      node = node.next();
    }

    res = res + "]";

    return res;
  }
예제 #10
0
  public static boolean deleteNode(LinkedListNode n) {

    if (n == null || n.next == null) return false;

    n.data = n.next.data;
    n.next = n.next.next;
    return true;
  }
예제 #11
0
 Boolean deleteMiddleGivenNode(LinkedListNode n) {
   if (n == null || n.next == null) {
     return false;
   }
   LinkedListNode nextNode = n.next;
   n.data = nextNode.data;
   n.next = nextNode.next;
   return true;
 }
예제 #12
0
 public void insert(LinkedListNode node) {
   if (head == null) {
     head = node;
     tail = node;
   } else {
     tail.next = node;
     tail = node;
   }
 }
예제 #13
0
 static void deleteFastNode(LinkedListNode node) {
   if (node == null) return;
   LinkedListNode node1 = node.next;
   if (node1 != null) {
     node.next = node1.next;
     node.value = node1.value;
   } else {
     node = null;
   }
 }
예제 #14
0
 public void offer(int val) {
   LinkedListNode node = new LinkedListNode(val);
   if (size == 0) {
     head = node;
     tail = node;
   } else {
     tail.next = node;
     tail = tail.next;
   }
   size++;
 }
예제 #15
0
  /** Erases all elements in the list and re-initializes it. */
  public void clear() {
    // Remove all references in the list.
    LinkedListNode node = getLast();
    while (node != null) {
      node.remove();
      node = getLast();
    }

    // Re-initialize.
    head.next = head.previous = head;
  }
예제 #16
0
 public LinkedListNode partition1(LinkedListNode node, int x) {
   LinkedListNode left = null, right = null;
   while (node != null) {
     LinkedListNode next = node.next;
     if (node.data < x) {
       node.next = left;
       left = node;
     } else {
       node.next = right;
       right = node;
     }
     node = next;
   }
   if (left == null) return right;
   LinkedListNode start = left;
   while (left.next != null) {
     left = left.next;
   }
   left.next = right;
   return start;
 }
예제 #17
0
 public void removeDuplicatesUsingMaps(LinkedListNode n) {
   HashSet<Integer> set = new HashSet<Integer>();
   LinkedListNode previous = null;
   while (n.next != null) {
     if (set.contains(n.data)) {
       previous.next = n.next;
     } else {
       set.add(n.data);
       previous = n;
     }
     n = n.next;
   }
 }
예제 #18
0
  /* Insert key and value into hash table. */
  public void put(K key, V value) {
    LinkedListNode<K, V> node = getNodeForKey(key);
    if (node != null) {
      node.value = value; // just update the value.
      return;
    }

    node = new LinkedListNode<K, V>(key, value);
    int index = getIndexForKey(key);
    if (arr.get(index) != null) {
      node.next = arr.get(index);
      node.next.prev = node;
    }
    arr.set(index, node);
  }
예제 #19
0
  public void removeDuplicatesUsingTwoPointers(LinkedListNode n) {
    LinkedListNode p1 = n;

    while (p1.next != null) {
      LinkedListNode p2 = p1;
      while (p2.next != null) {
        if (p2.next.data == p1.data) {
          p2.next = p2.next.next;
        } else {
          p2 = p2.next;
        }
      }
      p1 = p1.next;
    }
  }
예제 #20
0
 public static void main(String[] args) {
   LinkedListNode h1 = new LinkedListNode(1);
   LinkedListNode h2 = new LinkedListNode(5);
   LinkedListNode h3 = new LinkedListNode(3);
   LinkedListNode h4 = new LinkedListNode(9);
   LinkedListNode h5 = new LinkedListNode(2);
   LinkedListNode h6 = new LinkedListNode(8);
   h1.next = h2;
   h2.next = h3;
   h3.next = h4;
   h4.next = h5;
   h5.next = h6;
   h6.next = h3;
   CC205 c = new CC205();
   LinkedListNode res = new CC206().findBegin(h1);
   //		while(res!=null){
   //			System.out.println(res.data);
   //			res=res.next;
   //		}
   System.out.println(res.data);
 }
예제 #21
0
파일: Cache.java 프로젝트: noschinl/Smack
 /** Removes this node from the linked list that it is a part of. */
 public void remove() {
   previous.next = next;
   next.previous = previous;
 }
예제 #22
0
 /** Creates a new linked list. */
 public LinkedList() {
   head.next = head.previous = head;
 }
예제 #23
0
 /** Remove the value that is on the top of the stack */
 public void pop() {
   head = head.next();
 }