Package CtCILibrary

Examples of CtCILibrary.LinkedListNode


import CtCILibrary.LinkedListNode;

public class QuestionC {

  public static LinkedListNode partition(LinkedListNode node, int x) {
    LinkedListNode head = node;
    LinkedListNode tail = node;
   
    /* Partition list */
    while (node != null) {
      LinkedListNode next = node.next;
      if (node.data < x) {
        /* Insert node at head. */
        node.next = head;
        head = node;
      } else {
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, 8);
    System.out.println(h.printForward());
  }
View Full Code Here

      PartialSum sum = new PartialSum();
      return sum;
    }
    PartialSum sum = addListsHelper(l1.next, l2.next);
    int val = sum.carry + l1.data + l2.data;
    LinkedListNode full_result = insertBefore(sum.sum, val % 10);
    sum.sum = full_result;
    sum.carry = val / 10;
    return sum;
  }
View Full Code Here

    }
    PartialSum sum = addListsHelper(l1, l2);
    if (sum.carry == 0) {
      return sum.sum;
    } else {
      LinkedListNode result = insertBefore(sum.sum, sum.carry);
      return result;
    }
  } 
View Full Code Here

      return result;
    }
  } 
 
  private static LinkedListNode padList(LinkedListNode l, int padding) {
    LinkedListNode head = l;
    for (int i = 0; i < padding; i++) {
      LinkedListNode n = new LinkedListNode(0, null, null);
      head.prev = n;
      n.next = head;
      head = n;
    }
    return head;
View Full Code Here

    }
    return head;
  }
 
  private static LinkedListNode insertBefore(LinkedListNode list, int data) {
    LinkedListNode node = new LinkedListNode(data, null, null);
    if (list != null) {
      list.prev = node;
      node.next = list;
    }
    return node;
View Full Code Here

    }
    return value;
 
 
  public static void main(String[] args) {
    LinkedListNode lA1 = new LinkedListNode(3, null, null);
    LinkedListNode lA2 = new LinkedListNode(1, null, lA1);
    LinkedListNode lA3 = new LinkedListNode(5, null, lA2);
   
    LinkedListNode lB1 = new LinkedListNode(5, null, null);
    LinkedListNode lB2 = new LinkedListNode(9, null, lB1);
    LinkedListNode lB3 = new LinkedListNode(1, null, lB2)
   
    LinkedListNode list3 = addLists(lA1, lB1);
   
    System.out.println("  " + lA1.printForward());   
    System.out.println("+ " + lB1.printForward());     
    System.out.println("= " + list3.printForward())
   
    int l1 = linkedListToInt(lA1);
    int l2 = linkedListToInt(lB1);
    int l3 = linkedListToInt(list3);
   
View Full Code Here

      LinkedListNode l1, LinkedListNode l2, int carry) {
    if (l1 == null && l2 == null && carry == 0) {
             return null;
    }
   
    LinkedListNode result = new LinkedListNode();
    int value = carry;
    if (l1 != null) {
      value += l1.data;
    }
    if (l2 != null) {
      value += l2.data;
    }
    result.data = value % 10;
    if (l1 != null || l2 != null) {
      LinkedListNode more = addLists(l1 == null ? null : l1.next,
                       l2 == null ? null : l2.next,
                       value >= 10 ? 1 : 0);
      result.setNext(more);
    }
    return result;
View Full Code Here

    }
    return value + node.data;
 
 
  public static void main(String[] args) {
    LinkedListNode lA1 = new LinkedListNode(9, null, null);
    LinkedListNode lA2 = new LinkedListNode(9, null, lA1);
    LinkedListNode lA3 = new LinkedListNode(9, null, lA2);
   
    LinkedListNode lB1 = new LinkedListNode(1, null, null);
    LinkedListNode lB2 = new LinkedListNode(0, null, lB1);
    LinkedListNode lB3 = new LinkedListNode(0, null, lB2)
   
    LinkedListNode list3 = addLists(lA1, lB1, 0);
   
    System.out.println("  " + lA1.printForward());   
    System.out.println("+ " + lB1.printForward());     
    System.out.println("= " + list3.printForward())
   
    int l1 = linkedListToInt(lA1);
    int l2 = linkedListToInt(lB1);
    int l3 = linkedListToInt(list3);
   
View Full Code Here

import CtCILibrary.LinkedListNode;

public class Question {

  public static LinkedListNode FindBeginning(LinkedListNode head) {
    LinkedListNode slow = head;
    LinkedListNode fast = head;
   
    // Find meeting point
    while (fast != null && fast.next != null) {
      slow = slow.next;
      fast = fast.next.next;
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.