Package CtCILibrary

Examples of CtCILibrary.LinkedListNode


import CtCILibrary.LinkedListNode;

public class Question {
  public static void deleteDupsA(LinkedListNode n) {
    HashSet<Integer> set = new HashSet<Integer>();
    LinkedListNode previous = null;
    while (n != null) {
      if (set.contains(n.data)) {
        previous.next = n.next;
      } else {
        set.add(n.data);
View Full Code Here


    }
  }
 
  public static void deleteDupsC(LinkedListNode head) {
    if (head == null) return;
    LinkedListNode previous = head;
    LinkedListNode current = previous.next;
    while (current != null) {
      // Look backwards for dups, and remove any that you see.
      LinkedListNode runner = head;
      while (runner != current) {
        if (runner.data == current.data) {
          LinkedListNode tmp = current.next;
          previous.next = tmp;
          current = tmp;
          /* We know we can't have more than one dup preceding
           * our element since it would have been removed
           * earlier. */
 
View Full Code Here

  }
 
  public static void deleteDupsB(LinkedListNode head) {
    if (head == null) return;
   
    LinkedListNode current = head;
    while (current != null) {
      /* Remove all future nodes that have the same value */
      LinkedListNode runner = current;
      while (runner.next != null) {
        if (runner.next.data == current.data) {
          runner.next = runner.next.next;
        } else {
          runner = runner.next;
View Full Code Here

      current = current.next;
    }
  } 
 
  public static void main(String[] args) { 
    LinkedListNode first = new LinkedListNode(0, null, null); //AssortedMethods.randomLinkedList(1000, 0, 2);
    LinkedListNode head = first;
    LinkedListNode second = first;
    for (int i = 1; i < 8; i++) {
      second = new LinkedListNode(i % 2, null, null);
      first.setNext(second);
      second.setPrevious(first);
      first = second;
    }
    System.out.println(head.printForward());
    LinkedListNode clone = head.clone();
    deleteDupsA(head);
    System.out.println(head.printForward());
    deleteDupsC(clone);
  }
View Full Code Here

import CtCILibrary.LinkedListNode;

public class Question {

  public static LinkedListNode partition(LinkedListNode node, int x) {
    LinkedListNode beforeStart = null;
    LinkedListNode beforeEnd = null;
    LinkedListNode afterStart = null;
    LinkedListNode afterEnd = null;
   
    /* Partition list */
    while (node != null) {
      LinkedListNode next = node.next;
      node.next = null;
      if (node.data < x) {
        if (beforeStart == null) {
          beforeStart = node;
          beforeEnd = beforeStart;
View Full Code Here

  }
 
  public static void main(String[] args) {
    /* Create linked list */
    int[] vals = {1, 3, 7, 5, 2, 9, 4};
    LinkedListNode head = new LinkedListNode(vals[0], null, null);
    LinkedListNode current = head;
    for (int i = 1; i < vals.length; i++) {
      current = new LinkedListNode(vals[i], null, current);
    }
    System.out.println(head.printForward());
   
    /* Partition */
    LinkedListNode h = partition(head, 5);
   
    /* Print Result */
    System.out.println(h.printForward());
  }
 
View Full Code Here

import java.util.Stack;

public class QuestionB {
  public static boolean isPalindrome(LinkedListNode head) {
    LinkedListNode fast = head;
    LinkedListNode slow = head;
   
    Stack<Integer> stack = new Stack<Integer>();
   
    while (fast != null && fast.next != null) {
      stack.push(slow.data);
View Full Code Here

 
  public static void main(String[] args) {
    int length = 9;
    LinkedListNode[] nodes = new LinkedListNode[length];
    for (int i = 0; i < length; i++) {
      nodes[i] = new LinkedListNode(i >= length / 2 ? length - i - 1 : i, null, null);
    }
   
    for (int i = 0; i < length; i++) {
      if (i < length - 1) {
        nodes[i].setNext(nodes[i + 1]);
      }
      if (i > 0) {
        nodes[i].setPrevious(nodes[i - 1]);
      }
    }
    //nodes[length - 2].data = 9; // Uncomment to ruin palindrome
   
    LinkedListNode head = nodes[0];
    System.out.println(head.printForward());
    System.out.println(isPalindrome(head));
  }
View Full Code Here

import CtCILibrary.LinkedListNode;

public class QuestionB {

  public static LinkedListNode partition(LinkedListNode node, int x) {
    LinkedListNode beforeStart = null;
    LinkedListNode afterStart = null;
   
    /* Partition list */
    while (node != null) {
      LinkedListNode next = node.next;
      if (node.data < x) {
        /* Insert node into start of before list */
        node.next = beforeStart;
        beforeStart = node; 
      } else {
        /* Insert node into front of after list */
        node.next = afterStart;
        afterStart = node;
     
      node = next;
    }
   
    /* Merge before list and after list */
    if (beforeStart == null) {
      return afterStart;
    }
   
    LinkedListNode head = beforeStart;
    while (beforeStart.next != null) {
      beforeStart = beforeStart.next;
    }
    beforeStart.next = afterStart;
    return head;
View Full Code Here

 
  public static void main(String[] args) {
    int length = 20;
    LinkedListNode[] nodes = new LinkedListNode[length];
    for (int i = 0; i < length; i++) {
      nodes[i] = new LinkedListNode(i >= length / 2 ? length - i - 1 : i, null, null);
    }
   
    for (int i = 0; i < length; i++) {
      if (i < length - 1) {
        nodes[i].setNext(nodes[i + 1]);
      }
      if (i > 0) {
        nodes[i].setPrevious(nodes[i - 1]);
      }
    }
   
    LinkedListNode head = nodes[0];
    System.out.println(head.printForward());
   
    LinkedListNode h = partition(head, 7);
    System.out.println(h.printForward());
  }
View Full Code Here

TOP

Related Classes of CtCILibrary.LinkedListNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.