Package adt

Examples of adt.ListNode


    if (list == null || list.head == null) {
      return false;
    }
   
    // get the middle node
    ListNode fast = list.head;
    ListNode slow = list.head;
    ListNode prev = null;
   
    while (fast != null && fast.next != null) {
      prev = slow;
      slow = slow.next;
      fast = fast.next.next;
    }
   
    // 奇数长度, 记录下正中间的点
    // 1  2  3  2  1
    //    p  s     f
    if (fast != null) {
      slow = slow.next;
    }
   
    // 分割成两条链表
    prev.next = null;
   
    // 反转右边一半
    ListNode second_half = reverse(slow);
   
    return compareLists(list.head, second_half);
   
  }
View Full Code Here


  static ListNode reverse(ListNode head) {
    if (head == null || head.next == null) {
      return head;
    }
   
    ListNode first = head;
    ListNode rest = head.next;
   
    head = reverse(rest);
   
    first.next.next = first;
    first.next = null;
View Full Code Here

   
    if (l1 == null || l2 == null) {
      return false;
    }
   
    ListNode p1 = l1;
    ListNode p2 = l2;
   
    while (p1 != null && p2 != null) {
      if (p1.val == p2.val) {
        p1 = p1.next;
        p2 = p2.next;
View Full Code Here

  static void moveLastToFront(LinkedList list) {
    if (list == null || list.head == null) {
      return;
    }
   
    ListNode prev = null;
    ListNode node = list.head;
   
    while (node.next != null) {
      prev = node;
      node = node.next;
    }
View Full Code Here

public class Q035A_Swap_Kth_node_from_beginning_with_Kth_node_from_end_in_a_Singly_Linked_List {

  public static void main(String[] args) {
    LinkedList list = new LinkedList(8);
    ListNode head = swap(list.head, 3);
    head.print();
  }
View Full Code Here

      }
     
      // Find the kth node from beginning of linked list. We also find
      // previous of kth node because we need to update next pointer of
      // the previous.
      ListNode x = head;
      ListNode x_prev = null;
      for (int i = 1; i < k; i++)
      {
          x_prev = x;
          x = x.next;
      }
     
      // Similarly, find the kth node from end and its previous. kth node
      // from end is (n-k+1)th node from beginning
      ListNode y = head;
      ListNode y_prev = null;
      for (int i = 1; i < n-k+1; i++)
      {
          y_prev = y;
          y = y.next;
      }
     
      // If x_prev exists, then new next of it will be y. Consider the case
      // when y->next is x, in this case, x_prev and y are same. So the statement
      // "x_prev->next = y" creates a self loop. This self loop will be broken
      // when we change y->next.
      //
      //      x_prev   x           y_prev   y
      // 1  ->  2  ->  3  ->  4  ->  5  ->  6  ->  7  ->  8
      //
      //      x_prev   y  
      // 1  ->  2  ->  6  ->  7  ->  8
     
      if (x_prev != null)
          x_prev.next = y;
  
      // Same thing applies to y_prev
      //
      // x           y_prev
      // 3  ->  4  ->  5
      // |             |
      // ------ < ------
     
      if (y_prev != null)
          y_prev.next = x;
     
      // Swap next pointers of x and y. These statements also break self
      // loop if x->next is y or y->next is x
      //
      //      x_prev   y          y_prev   x
      // 1  ->  2  ->  6  ->  4 ->  5  ->  3  ->  7  ->  8
      //
     
      ListNode temp = x.next;
      x.next = y.next;
      y.next = temp;
     
      // Change head pointers when k is 1 or n
      if (k == 1)
View Full Code Here

      return head;
  }
 
  static int countNodes(ListNode head) {
    int n = 0;
    ListNode p = head;
    while (p != null) {
      n++;
      p = p.next;
    }
    return n;
View Full Code Here

public class Q003_Print_The_Middle_Of_A_Given_Linked_List {

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

      System.out.println("No such node");
    }
  }
 
  static ListNode getMiddleNode(ListNode head) {
    ListNode slow = head;
    ListNode fast = head;
   
    while (fast != null && fast.next != null) {
      slow = slow.next;
      fast = fast.next.next;
    }
View Full Code Here

public class Q017_Intersection_of_Two_Sorted_Linked_Lists {

  public static void main(String[] args) {
    LinkedList l1 = new LinkedList(new int[]{1, 2, 3, 4, 6});
    LinkedList l2 = new LinkedList(new int[]{2, 4, 6, 8});
    ListNode head = getIntersection(l1.head, l2.head);
    head.print();
  }
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.