How do i compare two linkedLists so that the onutcome is a sorted linked list from least to greates. Heres my code so far.
list1.txt
1 2 3 7 9 10 13

list2.txt
4 5 6 8 11 12 14

how do i compare the two and make it into a third sorted linked list?

import java.io.*;
   import java.util.Scanner;

    public class LinkedListDemo {
       public static void main(String[] args) throws IOException 
      {
         Scanner s = null;
         try {
            LinkedList test = new LinkedList();
				LinkedList test2 = new LinkedList();
            s = new Scanner(new BufferedReader(new FileReader("list1.txt")));
         
            while (s.hasNext()) 
            {
               test.add(s.next());
            }
				 System.out.println(test.toString());
				 
            s = new Scanner(new BufferedReader(new FileReader("list2.txt")));
         
            while (s.hasNext()) 
            {
               test2.add(s.next());
            }
				 System.out.println(test2.toString());


         } 
         finally 
         {
            if (s != null) 
            {
               s.close();
            }
                    
         }
      
      }
   }
/**
   The LinkedList class implements a Linked list.
*/

    class LinkedList
   {
    /**
       The Node class stores a list element
       and a reference to the next node.
    */
    
       private class Node
      {
         String value;   
         Node next;      
        
        /**
           Constructor.            
           @param val The element to store in the node.
           @param n The reference to the successor node.
        */
        
          Node(String val, Node n)
         {
            value = val;
            next = n;
         } 
        
        /**
           Constructor. 
           @param val The element to store in the node.
        */
        
          Node(String val)
         {
           // Call the other (sister) constructor.
            this(val, null);            
         }
      }	
    
      private Node first;  // list head
      private Node last;   // last element in list
        
    /**
       Constructor.
    */
    
       public LinkedList()
      {
         first = null;
         last = null;        
      }
    
    /**
       The isEmpty method checks to see 
   	 if the list is empty.
   	 @return true if list is empty, 
   	 false otherwise.
    */
    
       public boolean isEmpty()
      {        
         return first == null;       
      }
    
    /**
       The size method returns the length of the list.
       @return The number of elements in the list.
    */
    
       public int size()
      {
         int count = 0;
         Node p = first;     
         while (p != null)
         {
           // There is an element at p
            count ++;
            p = p.next;
         }
         return count;
      }
    
    /**
       The add method adds an element to
   	 the end of the list.
       @param e The value to add to the
   	 end of the list.       
    */
    
       public void add(String e)
      {
         if (isEmpty()) 
         {
            first = new Node(e);
            last = first;
         }
         else
         {
          // Add to end of existing list
            last.next = new Node(e);
            last = last.next;
         }      
      }
    
    /**
       The add method adds an element at a position.
       @param e The element to add to the list.
       @param index The position at which to add 
   	 the element.
       @exception IndexOutOfBoundsException When 
   	 index is out of bounds.  
    */
    
       public void add(int index, String e)
      {
         if (index < 0  || index > size()) 
         {
            String message = String.valueOf(index);
            throw new IndexOutOfBoundsException(message);
         }
         
         // Index is at least 0
         if (index == 0)
         {
             // New element goes at beginning
            first = new Node(e, first);
            if (last == null)
               last = first;
            return;
         }
         
         // Set a reference pred to point to the node that
         // will be the predecessor of the new node
         Node pred = first;        
         for (int k = 1; k <= index - 1; k++)        
         {
            pred = pred.next;           
         }
         
         // Splice in a node containing the new element
         pred.next = new Node(e, pred.next);  
         
         // Is there a new last element ?
         if (pred.next.next == null)
            last = pred.next;         
      }
    
    /**
       The toString method computes the string
       representation of the list.
       @return The string form of the list.
    */
    
       public String toString()
      {
         StringBuffer strBuffer = new StringBuffer();
      
      // Use p to walk down the linked list
         Node p = first;
         while (p != null)
         {
            strBuffer.append(p.value + "\n"); 
            p = p.next;
         }      
         return strBuffer.toString(); 
      }
   
  }

Recommended Answers

All 3 Replies

Add one to the other using a for loop or a for-each loop.
Use Collections.sort(linkedList) to sort it.

put the lists in a single arraylist(say arrlist) and use Collections.sort(arrlist)

edit: Sorry for the freepost, I was confused there for a second by the similarity of the previous poster's reply to my first reply.

Anyway, you don't need to create a third list, and really you can do it with either LinkedList or ArrayList since they both extend List. The Collections.sort method is defined as taking a List as its parameter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.