Adding items to end of linked list

后端 未结 9 851
夕颜
夕颜 2020-12-03 16:03

I\'m studying for an exam, and this is a problem from an old test:

We have a singly linked list with a list head with the following declaration:

clas         


        
9条回答
  •  無奈伤痛
    2020-12-03 16:57

    The addLast() needs some optimisation as the while loop inside addLast() has O(n) complexity. Below is my implementation of LinkedList. Run the code with ll.addLastx(i) once and run it with ll.addLast(i) again , you can see their is a lot of difference in processing time of addLastx() with addLast().

    Node.java

    package in.datastructure.java.LinkedList;
    
    /**
    * Created by abhishek.panda on 07/07/17.
    */
    public final class Node {
        int data;
        Node next;
    
        Node (int data){
           this.data = data;
        }
        public String toString(){
          return this.data+"--"+ this.next;
        }
    }
    

    LinkedList.java

    package in.datastructure.java.LinkedList;
    import java.util.ArrayList;
    import java.util.Date;
    
    public class LinkedList {
    
        Node head;
        Node lastx;
        /**
         * @description To append node at end looping all nodes from head
         * @param data
         */
        public void addLast(int data){
    
            if(head == null){
                head = new Node(data);
                return;
            }
            Node last = head;
            while(last.next != null) {
                last = last.next;
            }
            last.next = new Node(data);
        }
    
    
        /**
         * @description This keep track on last node and append to it
         * @param data
         */
        public void addLastx(int data){
    
            if(head == null){
                head = new Node(data);
                lastx = head;
                return;
            }
            if(lastx.next == null){
                lastx.next = new Node(data);
                lastx = lastx.next;
            }
    
        }
    
        public String toString(){
            ArrayList arrayList = new ArrayList(10);
            Node current = head;
            while(current.next != null) {
                arrayList.add(current.data);
                current = current.next;
            }
            if(current.next == null) {
                arrayList.add(current.data);
            }
            return arrayList.toString();
        }
    
    
        public static void main(String[] args) {
            LinkedList ll = new LinkedList();
            /**
             * @description Checking the code optimization of append code
             */
            Date startTime = new Date();
            for (int i = 0 ; i < 100000 ; i++){
                ll.addLastx(i);
            }
            Date endTime = new Date();
            System.out.println("To total processing time : " + (endTime.getTime()-startTime.getTime()));
            System.out.println(ll.toString());
        }
    
    }
    

提交回复
热议问题