// 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 insertAfter(final LinkedListNode existingNode, final LinkedListNode newNode) { if (newNode.getPrevious() != null || newNode.getNext() != null) { // do nothing if this node is already inserted somewhere return; } if (existingNode == null) { if (this.isEmpty()) { this.firstNode = newNode; this.lastNode = newNode; } else { // if existing node is null, then insert it as a first node final LinkedListNode node = this.firstNode; node.setPrevious(newNode); newNode.setNext(node); this.firstNode = newNode; } } else if (existingNode == this.lastNode) { existingNode.setNext(newNode); newNode.setPrevious(existingNode); this.lastNode = newNode; } else { (existingNode.getNext()).setPrevious(newNode); newNode.setNext(existingNode.getNext()); existingNode.setNext(newNode); newNode.setPrevious(existingNode); } this.size++; }
// find length of the loop public int findLoopLength(LinkedList list) { boolean cyclic = false; int length = 0; LinkedListNode slowPtr = list.getHead(), fastPtr = list.getHead(); while (fastPtr != null && slowPtr != null) { fastPtr = fastPtr.getNext(); if (fastPtr == slowPtr) { cyclic = true; break; } if (fastPtr == null) { break; } fastPtr = fastPtr.getNext(); if (fastPtr == slowPtr) { cyclic = true; break; } slowPtr = slowPtr.getNext(); } if (cyclic) { while (slowPtr != fastPtr) { slowPtr = slowPtr.getNext(); length++; } } return length; }
private LinkedListNode getLastNode(LinkedListNode node) { while (node.getNext() != null) { node = node.getNext(); } return node; }
/** 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); }
/** 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; }
// 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; }
/** * Removes a <code>LinkedListNode</code> from the list. This works by attach the previous * reference to the child reference. When the node to be removed is the first node it calls <code> * removeFirst()</code>. When the node to be removed is the last node it calls <code>removeLast() * </code>. * * @param node The <code>LinkedListNode</code> to be removed. */ public void remove(final LinkedListNode node) { if (this.firstNode == node) { removeFirst(); } else if (this.lastNode == node) { removeLast(); } else { node.getPrevious().setNext(node.getNext()); (node.getNext()).setPrevious(node.getPrevious()); this.size--; node.setPrevious(null); node.setNext(null); } }
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; }
/** 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); }
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()); }
/** 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; }
public static LinkedListNode NumbersAddition(LinkedListNode n1, LinkedListNode n2) { // number 1 and number 2 may not have the same number of digits. if (n1 == null || n2 == null) return null; LinkedListNode sumHead = null; LinkedListNode sumTail = null; int advance = 0; while (n1 != null || n2 != null) { int digitSum = 0; if (n1 != null && n2 != null) digitSum = n1.getData() + n2.getData() + advance; else if (n1 == null) digitSum = n2.getData() + advance; else digitSum = n1.getData() + advance; if (digitSum >= 10) { advance = 1; digitSum = digitSum % 10; } else { advance = 0; } LinkedListNode dSum = new LinkedListNode(); dSum.setData(digitSum); if (sumHead == null) sumHead = dSum; if (sumTail == null) { sumTail = dSum; } else { sumTail.setNext(dSum); sumTail = dSum; } // move n1 and n2 under certain conditions! if (n1 != null) n1 = n1.getNext(); if (n2 != null) n2 = n2.getNext(); } if (advance == 1) { LinkedListNode last = new LinkedListNode(); last.setData(advance); sumTail.setNext(last); sumTail = last; } return sumHead; }
public boolean equals(final Object object) { if (object == this) { return true; } if (object == null || !(object instanceof LinkedList)) { return false; } final LinkedList other = (LinkedList) object; if (this.size() != other.size()) { return false; } for (LinkedListNode thisNode = this.firstNode, otherNode = other.firstNode; thisNode != null && otherNode != null; thisNode = thisNode.getNext(), otherNode = otherNode.getNext()) { if (!thisNode.equals(otherNode)) { return false; } } return true; }
/** * Remove the first node from the list. The next node then becomes the first 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 removeFirst() { if (this.firstNode == null) { return null; } final LinkedListNode node = this.firstNode; this.firstNode = node.getNext(); node.setNext(null); if (this.firstNode != null) { this.firstNode.setPrevious(null); } else { this.lastNode = null; } this.size--; return 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; }
/** * Remove node following currentNode If no node exists (i.e., currentNode is the tail), do nothing */ public void deleteNext(LinkedListNode<T> currentNode) { // update pointer of the current node to the next next currentNode.setNext(currentNode.getNext().getNext()); }
/** Remove head node */ public void deleteFirst() { // change the pointer of the head pointer head = head.getNext(); }