Linked List Part 3: Deleting, Reversing, and Merging Additional List

In this section, we'll cover additional methods that enhance the functionality of the linked list. These methods include deleting nodes, reversing the order, and merging lists. Each of these operations introduces new ways to manipulate and manage the linked list. We'll also discuss the time and space complexity of each method.

  1. Deleting a Node at a Specific Index
  2. Reversing the Linked List
  3. Merging Two Linked Lists

1. Deleting a Node at a Specific Index

Method: deleteAtIndex(int index)

The deleteAtIndex method removes the node at a specified index from the linked list, provided the index is valid.

Implementation:

 
/*  Delete the indexth node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
       
        if( index < 0 || index >= this.size || this.head == null){
            throw new IndexOutOfBoundsException();
        }

        int count = 0;
        Node currentNode = this.head;
        Node prev = null;

        if(index == 0){
            this.head = this.head.next;
            if(this.head.next == null){
                this.tail = this.head;
            }
            this.size--;
            return;
        }

        while(currentNode != null){
           
            if(count == index && prev != null){
                prev.next = currentNode.next; //2 -> 1 -> 3
                currentNode = prev;
                if(currentNode.next == null){
                    this.tail = currentNode;
                }  
                break;
            }
           
            prev = currentNode;
            currentNode = currentNode.next;
            count++;  
        }

        this.size--;
    }

Time Complexity:

  • Best Case: O(1) if deleting the head node.
  • Average Case: O(n) due to traversal.
  • Worst Case: O(n) where n is the number of nodes in the list.

Space Complexity:

  • O(1): The method uses a constant amount of space regardless of the size of the list.

2. Reversing the Linked List

Method: reverse()

The reverse method reverses the order of the nodes in the linked list.

Implementation:


     /* Reverse order of the Linked list */
    public void reverse(){

        Node currentNode = this.head;
        Node prev = null;

        while(currentNode != null){
       
            Node temp = currentNode.next;
            currentNode.next = prev;
            prev = currentNode;
            currentNode = temp;

        }

        currentNode = this.tail;
        this.tail = this.head;
        this.head = currentNode;

    }

Time Complexity:

  • O(n): The method requires a single pass through the list to reverse it.

Space Complexity:

  • O(1): The method uses a constant amount of extra space regardless of the size of the list.

3. Merging Two Linked Lists

Method: merge(LinkedList list)

The merge method combines two linked lists into one.

Implementation:

public void merge (LinkedList list){

        if(list.getHead() == null){
            return;
        }

        if(this.head == null){
            this.head = list.getHead();
            this.size = list.size();
            return;
        }
       
        this.tail.next = list.getHead();
        this.size += list.size();
        this.tail = list.getTail();
       
    }

Time Complexity:

  • O(1): The merge operation is constant time when appending to the end of the list.

Space Complexity:

  • O(1): The method uses a constant amount of space regardless of the size of the lists being merged.

Space Complexity:

  • O(1): The method uses a constant amount of space, with k being handled by the helper function.

What is next?

Sorting the linked list using divide and conquer algorithm. 


Top Posts

Popular Methods for Machine Learning

Big Data Characteristics Explained

Planning a Data Warehouse