Пример #1
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;
 }
Пример #2
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;
  }
Пример #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 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;
 }
Пример #5
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;
  }
Пример #6
0
  private LinkedListNode getLastNode(LinkedListNode node) {

    while (node.getNext() != null) {
      node = node.getNext();
    }

    return node;
  }
Пример #7
0
 public int hashCode() {
   final int PRIME = 31;
   int result = 1;
   for (LinkedListNode node = this.firstNode; node != null; node = node.getNext()) {
     result = PRIME * result + node.hashCode();
   }
   return result;
 }
Пример #8
0
 /**
  * Returns a String representation of the linked list with a comma delimited list of all the
  * elements in the list.
  *
  * @return a String representation of the LinkedList.
  */
 public String toString() {
   LinkedListNode node = head.next;
   StringBuilder buf = new StringBuilder();
   while (node != head) {
     buf.append(node.toString()).append(", ");
     node = node.next;
   }
   return buf.toString();
 }
Пример #9
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;
 }
Пример #10
0
 /** Remove tail node */
 public void deleteLast() {
   // set a LinkedListNode to get the current Node
   LinkedListNode<T> currentNode = head;
   // when the next to next node is null
   while (currentNode.getNext().getNext() != null) {
     currentNode = currentNode.getNext();
   }
   // set the current node to be null
   currentNode.setNext(null);
 }
Пример #11
0
 /** Insert a new node with data after currentNode */
 public void insertAfter(LinkedListNode<T> currentNode, T data) {
   // create a new linkedListNode
   LinkedListNode<T> newNode = new LinkedListNode<T>();
   // assign that node some data
   newNode.setData(data);
   // point new node where current pointer is
   newNode.setNext(currentNode.getNext());
   // set head pointer to this new node
   currentNode.setNext(newNode);
 }
Пример #12
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;
   }
 }
Пример #13
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);
 }
 public static void traverse(LinkedListNode head) {
   if (head == null) return;
   StringBuffer buffer = new StringBuffer();
   while (head != null) {
     buffer.append(head.getData() + "->");
     // remember to move
     head = head.getNext();
   }
   buffer.append("null");
   System.out.println(buffer.toString());
 }
Пример #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
 // Floyd's algorithm
 public boolean isCyclic(LinkedList list) {
   LinkedListNode slowPtr = list.getHead(), fastPtr = list.getHead();
   while (fastPtr != null && slowPtr != null) {
     fastPtr = fastPtr.getNext();
     if (fastPtr == slowPtr) return true;
     if (fastPtr == null) return false;
     fastPtr = fastPtr.getNext();
     if (fastPtr == slowPtr) return true;
     slowPtr = slowPtr.getNext();
   }
   return false;
 }
Пример #17
0
 /** Return the number of nodes in this list. */
 public int size() {
   // set a LinkedListNode to get the current Node
   LinkedListNode<T> currentNode = head;
   int listSize = 0;
   // As long as there is a node with data after the current Node
   while (currentNode != null) {
     listSize = listSize + 1;
     // shift to set the next node as the current Node
     currentNode = currentNode.getNext();
   }
   return listSize;
 }
Пример #18
0
 /** Get the tail node of the list. */
 public LinkedListNode<T> getLastNode() {
   // set a LinkedListNode to get the current Node
   LinkedListNode<T> currentNode = head;
   // As long as there is a node with data after the current Node
   while (currentNode.getNext() != null) {
     // shift to set the next node as the current Node
     currentNode = currentNode.getNext();
   }
   // When there is no node after the current Node return the data in the
   // current Node
   return currentNode;
 }
Пример #19
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;
   }
 }
Пример #20
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);
  }
Пример #21
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;
    }
  }
Пример #22
0
 /**
  * Remove the last node from the list. The previous node then becomes the last node. If this is
  * the last node then both first and last node references are set to null.
  *
  * @return The first <code>LinkedListNode</code>.
  */
 public LinkedListNode removeLast() {
   if (this.lastNode == null) {
     return null;
   }
   final LinkedListNode node = this.lastNode;
   this.lastNode = node.getPrevious();
   node.setPrevious(null);
   if (this.lastNode != null) {
     this.lastNode.setNext(null);
   } else {
     this.firstNode = this.lastNode;
   }
   this.size--;
   return node;
 }
Пример #23
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;
   }
 }
Пример #24
0
 /** Return a String representation of the list. */
 public String toString() {
   // set a LinkedListNode to get the current Node
   LinkedListNode<T> currentNode = head;
   String s = "{";
   // As long as there is a node with data after the current Node
   while (currentNode != null) {
     s = s + " " + currentNode.getData();
     // shift to set the next node as the current Node
     currentNode = currentNode.getNext();
   }
   // When there is no node after the current Node return the data in the
   // current Node
   s = s + "}";
   System.out.println("The list is " + s);
   return s;
 }
Пример #25
0
  public static void main(String[] args) {
    LinkedListNode node = method.randomLinkedList(10, 0, 10);
    System.out.println(node.printForward());
    int nth = 2;
    LinkedListNode n = nthNode(node, nth);
    try {
      FileWriter write = new FileWriter("findn.txt");
      PrintWriter out = new PrintWriter(write);
      if (n != null) out.println(nth + "th to last node is " + n.data);
      else out.println("null");

      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #26
0
 public void Search(String s) // Returns nothing if its empty, else goes to LLN to search.
     {
   if (head != null) head.Search(s);
   if (head == null)
     System.out.println(s + " wasn't found but would be at the beginning of the list.");
   System.out.println();
 }
Пример #27
0
 public String printForward() {
   String data = "(" + key + "," + value + ")";
   if (next != null) {
     return data + "->" + next.printForward();
   } else {
     return data;
   }
 }
Пример #28
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;
  }
Пример #29
0
  // finds the starting node of the cycle
  public LinkedListNode findLoopStart(LinkedList list) {
    LinkedListNode slowPtr = list.getHead(), fastPtr = list.getHead();
    boolean cyclic = false;
    while (slowPtr != null && fastPtr != null) {
      fastPtr = fastPtr.getNext();
      if (fastPtr == slowPtr) {
        cyclic = true;
        break;
      }
      if (fastPtr == null) {
        cyclic = false;
        break;
      }
      fastPtr = fastPtr.getNext();
      if (fastPtr == slowPtr) {
        cyclic = true;
        break;
      }
      slowPtr = slowPtr.getNext();
    }

    // if cycle exists
    if (cyclic) {
      slowPtr = list.getHead();
      while (slowPtr != fastPtr) {
        slowPtr = slowPtr.getNext();
        fastPtr = fastPtr.getNext();
      }
      return slowPtr;
    }
    return null;
  }
Пример #30
0
 public void insert(LinkedListNode node) {
   if (head == null) {
     head = node;
     tail = node;
   } else {
     tail.next = node;
     tail = node;
   }
 }