Package adt

Examples of adt.ListNode


  static ListNode getIntersection(ListNode h1, ListNode h2) {
    if (h1 == null || h2 == null) {
      return null;
    }
   
    ListNode head = new ListNode();
    ListNode prev = head;
   
    ListNode n1 = h1, n2 = h2;
   
    while (n1 != null && n2 != null) {
      if (n1.val == n2.val) {
        ListNode node = new ListNode(n1.val);
        prev.next = node;
        prev = node;
       
        n1 = n1.next;
        n2 = n2.next;
View Full Code Here


  public static void main(String[] args) {
   
  }
 
  static ListNode insertSortedCLL(ListNode head, ListNode node) {
    ListNode current = head;
   
    // case 1, linkes list is empty
    if (head == null) {
      node.next = node;
      head = node;
View Full Code Here

  static void copyLinkedList(LinkedList list) {
    if (list == null || list.head == null) {
      return;
    }
   
    ListNode node = list.head;
    ListNode copy = null;
    ListNode next = null;
   
    // step 1. insert copy nodes after each original node
    while (node != null) {
      next = node.next;
      copy = new ListNode(node.val);
      copy.next = next;
      node.next = copy;
      node = next;
    }
   
    list.print();
   
    // step 2. copy arbit pointer
    node = list.head;
   
    while (node != null && node.next != null) {
      next = node.next.next;
      //node.next.arbit = node.arbit.next;
      node = next;
    }
   
    // step 3. cut the linked list
    node = list.head;
    ListNode copy_head = node.next;
   
    while (node != null && node.next != null) {
      copy = node.next;
      next = node.next.next;
     
      node.next = next;
      copy.next = (copy.next == null) ? null : copy.next.next;
     
      node = next;
    }
   
    copy_head.print();
  }
View Full Code Here

      node.next = node.next.next;
      return;
    }
   
    // then we have to find the node from the head
    ListNode curr = head;
   
    while (curr.next != node) {
      curr = curr.next;
    }
   
View Full Code Here

public class Q004_Nth_Node_From_The_End_Of_A_Linked_List {
 
  public static void main(String[] args) {
    LinkedList list = new LinkedList(10);
   
    ListNode node = lastNthNode(list.head, 3);
   
    if (node != null) {
      System.out.println(node.val);
    } else {
      System.out.println("No such node");
View Full Code Here

      System.out.println("No such node");
    }
  }
 
  static ListNode lastNthNode(ListNode head, int n) {
    ListNode slow = head;
    ListNode fast = head;
   
    // fast先走n步
    int count = 0;
    while (fast != null && count < n) {
      count++;
View Full Code Here

public class Q007_Detect_Loop_In_A_Linked_List {

  public static void main(String[] args) {
    LinkedList list = new LinkedList();
   
    ListNode node1 = list.append(1);
    ListNode node2 = list.append(2);
    ListNode node3 = list.append(3);
    ListNode node4 = list.append(4);
    ListNode node5 = list.append(5);
   
    // make a loop
    node5.next = node2;
   
    boolean res = detectLoop(list);
View Full Code Here

  static boolean detectLoop(LinkedList list) {
    if (list == null || list.head == null) {
      return false;
    }
   
    ListNode head = list.head;
    ListNode fast = head;
    ListNode slow = head;
   
    while (fast != null && fast.next != null) {
      slow = slow.next;
      fast = fast.next.next;
     
View Full Code Here

    if (head.next == null) {
      return new TreeNode(head.val);
    }
   
    // step 1. find the middle node
    ListNode prev = null;
    ListNode slow = head;
    ListNode fast = head;
   
    while (fast != null && fast.next != null) {
      prev = slow;
      slow = slow.next;
      fast = fast.next.next;
View Full Code Here

  static void pairSwap(LinkedList list) {
    if (list == null) {
      return;
    }
   
    ListNode node = list.head;
   
    while (node != null && node.next != null) {
      swap(node, node.next);
      node = node.next.next;
    }
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.