Design Linked List - The Coding Shala

Home >> Data Structure >> Design Linked List

Design Linked List

Problem:

Design your implementation of the linked list. You can choose to use them a singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: Val and next. Val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.



Implement these functions in your linked list class:



get(index): Get the value of the index-th node in the linked list. If the index is invalid, return -1.

addAtHead(val): Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.

addAtTail(val): Append a node of value val to the last element of the linked list.

addAtIndex(index, val): Add a node of value val before the index-th node in the linked list. If the index equals the length of the linked list, the node will be appended to the end of the linked list. If the index is greater than the length, the node will not be inserted. If the index is negative, the node will be inserted at the head of the list.

deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:



MyLinkedList linkedList = new MyLinkedList();

linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2);  // linked list becomes 1->2->3
linkedList.get(1);            // returns 2
linkedList.deleteAtIndex(1);  // now the linked list is 1->3
linkedList.get(1);            // returns 3
Note:

All values will be in the range of [1, 1000].
The number of operations will be in the range of [1, 1000].
Please do not use the built-in LinkedList library.

Design Linked List Java Solution

Approach
we can use a singly linked list or doubly linked list.

Java 

class MyLinkedList {
    
    class Node{
        int value;
        Node next;
        Node(int val){
            this.value = val;
        }
    }
    
    Node head;
    int size;
    
    /** Initialize your data structure here. */
    public MyLinkedList() {
        head = null;
        size = 0;
    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
       if(index < 0 || index >= size) return -1;
        Node tmp = head;
        for(int i = 0; i<=size; i++){
            if(i == index){
                return tmp.value;
            }
            tmp = tmp.next;
        }
        return -1;
    }
    
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        Node nw = new Node(val);
        nw.next = head;
        head = nw;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        Node nw = new Node(val);
        Node tmp = head;
        for(int i = 0; i<size-1;i++) tmp = tmp.next;
        tmp.next = nw;
        nw.next = null;
        size++;
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        Node nw = new Node(val);
        if(index > size) return;
        if(index == 0 || index<0){
            nw.next = head;
            head = nw;
            size++;
        }else if(index == size){
            Node tmp = head;
            while(tmp.next != null) tmp = tmp.next;
            tmp.next = nw;
            nw.next = null;
            size++;
        }else{
            Node tmp = head;
            for(int i = 0; i<=index; i++){
                if(i == index-1){
                    nw.next = tmp.next;
                    tmp.next = nw;
                    size++;
                }
                tmp = tmp.next;
            }
        }
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
       if(index <0) return;
        Node tmp = head;
        int i = 0;
        if(index == 0) { head = head.next; size--; }
        else{
        while(i<index-1 && i<size){
            tmp = tmp.next;
            i++;
        }
        if(i < size && tmp.next != null){
            if(tmp.next.next == null) tmp.next = null;
            else{
                tmp.next = tmp.next.next;
            }
            size--;
        }
       }
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */


Approach 2:
Using the doubly linked list.

Java
class MyLinkedList {
    //using doubly linked list
    class Node{
        int value;
        Node next,prev;
        Node(int val){
            this.value = val;
        }
    }
    
    Node head;
    int size;
    
    /** Initialize your data structure here. */
    public MyLinkedList() {
        head = null;
        size = 0;
    }
    
    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
       if(index < 0 || index >= size) return -1;
        Node tmp = head;
        for(int i = 0; i<=size; i++){
            if(i == index){
                return tmp.value;
            }
            tmp = tmp.next;
        }
        return -1; 
    }
    
    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        Node nw = new Node(val);
        nw.next = head;
        nw.prev = null;
        head = nw;
        size++;
    }
    
    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        Node nw = new Node(val);
        Node tmp = head;
        for(int i = 0; i<size-1;i++) tmp = tmp.next;
        tmp.next = nw;
        nw.next = null;
        nw.prev = tmp;
        size++;
    }
    
    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        Node nw = new Node(val);
        if(index > size) return;
        if(index == 0 || index<0){
            nw.next = head;
            nw.prev = null;
            head = nw;
            size++;
        }else if(index == size){
            Node tmp = head;
            while(tmp.next != null) tmp = tmp.next;
            tmp.next = nw;
            nw.next = null;
            nw.prev = tmp;
            size++;
        }else{
            Node tmp = head;
            for(int i = 0; i<=index; i++){
                if(i == index-1){
                    nw.next = tmp.next;
                    nw.prev = tmp;
                    tmp.next.prev = nw;
                    tmp.next = nw;
                    size++;
                }
                tmp = tmp.next;
            }
        } 
    }
    
    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
       if(index <0) return;
        Node tmp = head;
        int i = 0;
        if(index == 0) { head = head.next; size--; }
        else{
        while(i<index-1 && i<size){
            tmp = tmp.next;
            i++;
        }
        if(i < size && tmp.next != null){
            if(tmp.next.next == null) tmp.next = null; //check here
            else{
                tmp.next = tmp.next.next;
                tmp.next.prev = tmp;
            }
            size--;
        }
       } 
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */



Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Java Program to Convert Binary to Decimal - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Java Program to Convert Decimal to Binary - The Coding Shala