Package adt

Examples of adt.ListNode


public class Q025_Add_Two_Numbers_Represented_By_Linked_Lists {
 
  public static void main(String[] args) {
    LinkedList a = new LinkedList(new int[]{5, 6, 3, 4});
    LinkedList b = new LinkedList(new int[]{8, 4, 2, 9});
    ListNode c = add(a.head, b.head);
    c.print();
  }
View Full Code Here


    c.print();
  }
 
  static ListNode add(ListNode a, ListNode b) {
    // create a fake head
    ListNode fc = new ListNode();
    ListNode pc = fc;
   
    int carry = 0, sum = 0;
   
    ListNode node = null;
   
    while (a != null || b != null) {
      sum = ((a == null) ? 0 : a.val) + ((b == null) ? 0 : b.val) + carry;
      carry = sum / 10;
      sum = sum % 10;
     
      node = new ListNode(sum);
      pc.next = node;
      pc = node;
     
      if (a != null) {
        a = a.next;
      }
     
      if (b != null) {
        b = b.next;
      }
    }
   
    if (carry > 0) {
      node = new ListNode(carry);
      pc.next = node;
      pc = node;
    }
   
    pc.next = null;
View Full Code Here

  static void skipMdeleteN(ListNode head, int M, int N) {
    if (head == null) {
      return;
    }
   
    ListNode curr = head;
   
    while (curr != null) {
      // skip M nodes
      for (int i = 1; i < M && curr != null; i++) {
        curr = curr.next;
      }
     
      // If we reached end of list, then return
      if (curr == null) {
        return;
      }
     
      // Start from next node and delete N nodes
      ListNode t = curr.next;
      for (int i = 1; i <= N && t != null; i++) {
        t = t.next;
      }
     
      // Link the previous list with remaining nodes
View Full Code Here

  static void removeDups(LinkedList list) {
    if (list == null || list.head == null) {
      return;
    }
   
    ListNode slow = list.head;
    ListNode fast = slow.next;
   
    while (fast != null) {
      if (fast.val != slow.val) {
        slow.next = fast;
        slow = fast;
View Full Code Here

 
  /**
   * Insert new node to the end
   */
  public ListNode append(int x) {
    ListNode node = new ListNode(x);
   
    if (head == null) {
      head = node;
      tail = node;
    } else {
View Full Code Here

 
  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
   
    ListNode p = head;
    while (p != null) {
      sb.append(p.val);
      p = p.next;
     
      if (p != null) {
View Full Code Here

  // ---------------------------
  //  Merge Sort
  // ---------------------------
 
  public void mergeSort(boolean ascending) {
    ListNode h = head;
    head = mergeSort(h, ascending);
    print();
  }
View Full Code Here

    if (h == null || h.next == null) {
      return h;
    }
   
    // find the middle node
    ListNode prev = null;
    ListNode slow = h;
    ListNode fast = h;
   
    while (fast != null && fast.next != null) {
      prev = slow;
      slow = slow.next;
      fast = fast.next.next;
    }
   
    prev.next = null;
   
    ListNode left = mergeSort(h, ascending);
    ListNode right = mergeSort(slow, ascending);
   
    return mergeLists(left, right, ascending);
  }
View Full Code Here

   
    return mergeLists(left, right, ascending);
  }
 
  private ListNode mergeLists(ListNode l1, ListNode l2, boolean ascending) {
    ListNode fakeHead = new ListNode();
    ListNode p = fakeHead;
   
    ListNode a = l1, b = l2;
   
    if (ascending) {
      while (a != null && b != null) {
        if (a.val < b.val) {
          p.next = a;
View Full Code Here

   
    findSumTriple(a.head, b.head, c.head, 101);
  }
 
  static void findSumTriple(ListNode l1, ListNode l2, ListNode l3, int target) {
    ListNode a = l1, b, c;
   
    while (a != null) {
      b = l2;
      c = l3;
     
View Full Code Here

TOP

Related Classes of adt.ListNode

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.