Package adt

Examples of adt.ListNode


public class Q010_Insert_In_Sorted_List {
 
  public static void main(String[] args) {
    int[] arr = {1, 2, 3, 5, 7, 9};
    LinkedList list = new LinkedList(arr);
    insert(list, new ListNode(6));
  }
View Full Code Here


  static void insert(LinkedList list, ListNode node) {
    if (list == null || node == null) {
      return;
    }
   
    ListNode head = list.head;
   
    if (head == null || head.val >= node.val ) {
      node.next = head;
      head = node;
    } else {
      ListNode current = head;
      while (current.next != null && current.next.val < node.val) {
        current = current.next;
      }
      node.next = current.next;
      current.next = node;
View Full Code Here

  static ListNode reverse(ListNode head, int k) {
    if (head == null) {
      return null;
    }
   
    ListNode curr = head;
    ListNode next = null;
    ListNode prev = null;
   
    int count = 0;
   
    // reverse first k nodes of the linked list
    while (curr != null && count < k) {
View Full Code Here

public class Q040_Reverse_alternate_nodes_and_append_at_the_end {

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

  static ListNode rearrange(ListNode head) {
    if (head == null) {
      return null;
    }
   
    ListNode fa = new ListNode();
    ListNode pa = fa, pb = null;
   
    ListNode curr = head, next = null;
   
    int count = 1;
    while (curr != null) {
      next = curr.next;
     
View Full Code Here

  static void splitList(LinkedList list) {
    if (list == null) {
      return;
    }
   
    ListNode a = new ListNode();
    ListNode pa = a;
    ListNode b = new ListNode();
    ListNode pb = b;
   
    ListNode node = list.head;
    while (node != null) {
      if (node.val == 0) {
        pa.next = node;
        pa = node;
      } else {
View Full Code Here

  public static void main(String[] args) {
    LinkedList a = new LinkedList(new int[]{10, 15, 4, 20});
    LinkedList b = new LinkedList(new int[]{8, 4, 2, 10});
   
    ListNode intersection = getIntersection(a.head, b.head);
    ListNode union = getUnion(a.head, b.head);
   
    intersection.print();
    union.print();
  }
View Full Code Here

 
  // ----------------------------
  //  Intersection
  // ----------------------------
  static ListNode getIntersection(ListNode l1, ListNode l2) {
    ListNode a = l1;
    ListNode b = l2;
   
    // use a hashmap to store visited nodes
    Set<Integer> set = new HashSet<Integer>();
   
    // use a fake head
    ListNode fh = new ListNode();
    ListNode p = fh;
   
    // step 1. go through list a
    while (a != null) {
      // avoid duplication
      if (!set.contains(a.val)) {
View Full Code Here

 
  // ----------------------------
  //  Union
  // ----------------------------
  static ListNode getUnion(ListNode l1, ListNode l2) {
    ListNode a = l1;
    ListNode b = l2;
   
    // use a hashmap to store visited nodes
    Set<Integer> set = new HashSet<Integer>();
   
    // use a fake head
    ListNode fh = new ListNode();
    ListNode p = fh;
   
    // step 1. go through list a
    while (a != null) {
      if (!set.contains(a.val)) {
        set.add(a.val);
View Full Code Here

  // ----------------------
  //  Iterative
  // ----------------------
 
  static void reverse(LinkedList list) {
    ListNode head = list.head;
   
    ListNode prev = null;
    ListNode curr = head;
    ListNode next = null;
   
    while (curr != null) {
      next = curr.next;
      curr.next = prev;
      prev = curr;
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.