Package adt

Examples of adt.ListNode


  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


  static void leftRotate(LinkedList list, int k) {
    if (list == null) {
      return;
    }
   
    ListNode head = list.head;
    ListNode current = head;
   
    int count = 1;
    while (current != null && count < k) {
      count++;
      current = current.next;
    }
   
    // If current is NULL, k is greater than or equal to count
      // of nodes in linked list. Don't change the list in this case
    if (current == null) {
      return;
    }
   
    // current points to kth node. Store it in a variable.
      // kthNode points to node 40 in the above example
    ListNode kthNode = current;
   
    // current will point to last node after this loop
      // current will point to node 60 in the above example
    while (current.next != null) {
      current = current.next;
View Full Code Here

  // Main function that inserts nodes of linked list q into p at alternate
  // positions. Since head of first list never changes and head of second list
  // may change, we need single pointer for first list and double pointer for
  // second list.
  static void merge(ListNode a, ListNode b) {
    ListNode a_curr = a, b_curr = b;
    ListNode a_next = null, b_next = null;
   
    while (a_curr != null && b_curr != null) {
      // Save next pointers
      a_next = a_curr.next;
      b_next = b_curr.next;
View Full Code Here

    // If linked list is empty or there is only one node in list
      if (head == null || head.next == null)
          return;
     
      // Initialize previous and current pointers
      ListNode prev = head;
      ListNode curr = prev.next;
     
      head = curr;  // Change head before proceeeding
     
      // Traverse the list
      while (true)
      {
          ListNode next = curr.next;
          curr.next = prev; // Change next of current as previous node
  
          // If next NULL or next is the last node
          if (next == null || next.next == null)
          {
View Full Code Here

      // Base Case: The list is empty or has only one node
      if (head == null || head.next == null)
          return head;
  
      // Store head of list after two nodes
      ListNode rest = head.next.next;
  
      // Change head
      ListNode newhead = head.next;
  
      // Change next of second node
      head.next.next = head;
  
      // Recur for remaining list and change next of head
View Full Code Here

public class Q024_Segregate_Even_And_Odd_Nodes {

  public static void main(String[] args) {
    LinkedList list = new LinkedList(new int[]{17, 15, 8, 12, 10, 5, 4, 1, 7, 6});
    ListNode head = segregate(list.head);
    head.print();
  }
View Full Code Here

    head.print();
  }
 
  static ListNode segregate(ListNode head) {
    // create fake heads
    ListNode fa = new ListNode();
    ListNode pa = fa;
   
    ListNode fb = new ListNode();
    ListNode pb = fb;
   
    ListNode node = head;
   
    while (node != null) {
      if (node.val % 2 == 0) {
        pa.next = node;
        pa = node;
View Full Code Here

public class Q020_Merge_Two_Sorted_Linked_Lists {

  public static void main(String[] args) {
    LinkedList a = new LinkedList(new int[]{5, 10, 15});
    LinkedList b = new LinkedList(new int[]{2, 3, 20});
    ListNode c = merge(a.head, b.head);
   
    if (c != null) {
      c.print();
    }
  }
View Full Code Here

  //  Iterative
  // ---------------------------
 
  static ListNode merge(ListNode l1, ListNode l2) {
    // use a fake head
    ListNode head = new ListNode();
    ListNode p = head;
   
    while (l1 != null && l2 != null) {
      if (l1.val < l2.val) {
        p.next = l1;
        p = l1;
View Full Code Here

   
    if (l2 == null) {
      return l1;
    }
   
    ListNode res;
   
    if (l1.val < l2.val) {
      res = l1;
      res.next = mergeR(l1.next, l2);
    } else {
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.