// 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 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; }
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 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 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; } }
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; } }
/** * 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; }
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; }
@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; }
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; }
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; }
public void insert(LinkedListNode node) { if (head == null) { head = node; tail = node; } else { tail.next = node; tail = node; } }
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 void offer(int val) { LinkedListNode node = new LinkedListNode(val); if (size == 0) { head = node; tail = node; } else { tail.next = node; tail = tail.next; } size++; }
/** 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; }
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; }
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; } }
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); }
/** Removes this node from the linked list that it is a part of. */ public void remove() { previous.next = next; next.previous = previous; }
/** Creates a new linked list. */ public LinkedList() { head.next = head.previous = head; }
/** Remove the value that is on the top of the stack */ public void pop() { head = head.next(); }