/** * 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; }
// 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; }
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)); }
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; }
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; }
private LinkedListNode getLastNode(LinkedListNode node) { while (node.getNext() != null) { node = node.getNext(); } return node; }
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; }
/** * 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(); }
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; }
/** 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); }
/** 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); }
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; } }
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()); }
/** 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; }
// 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; }
/** 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; }
/** 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; }
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; } }
/* 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); }
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; } }
/** * 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; }
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; } }
/** 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; }
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(); } }
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(); }
public String printForward() { String data = "(" + key + "," + value + ")"; if (next != null) { return data + "->" + next.printForward(); } else { return data; } }
@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; }
// 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; }
public void insert(LinkedListNode node) { if (head == null) { head = node; tail = node; } else { tail.next = node; tail = node; } }