Advertisement
gelita

linkedLIst listnode

Feb 20th, 2020
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.30 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class ListNode {
  4.   public int value;
  5.   public ListNode next;
  6.  
  7.   public ListNode(int value){
  8.     this.value = value;
  9.   }
  10. }
  11.  
  12. class LinkedList {
  13.   public int length = 0;
  14.   public ListNode head;
  15.   public ListNode tail;
  16.  
  17.   // Time Complexity: O(N)
  18.   // Auxiliary Space Complexity: O()
  19.   public void append(int value){
  20.    //add node to the end
  21.    insert(value, length);
  22.   }
  23.  
  24.  
  25.   // Time Complexity: O(N)
  26.   // Auxiliary Space Complexity: O(1)
  27.   public void insert(int value, int index){
  28.     //check to see if index for insertion is out of bounds of the LinkedList
  29.     if(index > length || index < 0){
  30.       return;
  31.     }
  32.     ListNode newNode = new ListNode(value);
  33.     //if no nodes exist, new node is head AND tail
  34.     if(length == 0){      
  35.       head = newNode;
  36.       tail = newNode;      
  37.     }else if(index == 0){
  38.       //set new node to pt to curr head next
  39.       //set head to pt to new node
  40.       newNode.next = head;
  41.       head = newNode;
  42.     }else if(index == length){
  43.       tail.next = newNode;
  44.       tail = newNode;
  45.     }else{
  46.       //if inserting somewhere in the middle->
  47.       //make iterator node variable and set to head of LinkedList
  48.       ListNode previousNode = head;
  49.       for(int i = 0; i< index-1; i++){
  50.         previousNode = previousNode.next;
  51.       }
  52.       //once node previous to index, insert newNode
  53.       newNode.next = previousNode.next;
  54.       previousNode.next = newNode;
  55.     }
  56.     //increase length to acct for inserted node
  57.     length++;    
  58.   }
  59.  
  60.  
  61.   // Time Complexity: O(N)
  62.   // Auxiliary Space Complexity: O(1)
  63.    public void delete(int index){
  64.    
  65.     //out of bounds? return
  66.     if(index < 0 || index >= length){
  67.       return;
  68.     }
  69.     //if there is only 1 item in LinkedList
  70.     if(length == 1){
  71.       head = null;
  72.       tail = null;
  73.     }else if(index == 0){
  74.       head = head.next;
  75.     }else {
  76.       //create iterator
  77.       ListNode previousNode = head;
  78.       for(int i = 0 ; i <index-1; i++){
  79.         previousNode = previousNode.next;
  80.       }
  81.       previousNode.next = previousNode.next.next;      
  82.       if(index == length -1){
  83.         tail = previousNode;
  84.       }
  85.     }
  86.     length--;
  87.   }
  88.  
  89.  
  90.   // Time Complexity: O(N)
  91.   // Auxiliary Space Complexity: O(1)
  92.   public boolean contains(int value){
  93.    
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement