1

I'm trying to understand LinkedLists(Single LinkedList to be precise).

I heard/read that delete and add operation will be performed with O(1) complexity and I'm still not getting how to implement with O(1) complexity for these two operation. Below is my implementation in java(NOTE: I don't know c, c++ coding, So I recently started understanding data structures).

public class Node
{
    private Integer data    = null;
    private Node    next    = null;
    private int     size    = 0;

    public Node()
    {

    }

    private Node(Integer data)
    {
        this.data = data;
    }

    public boolean add(Integer data)
    {
        if (null == data) return false;
        if (null == this.data)
        {
            this.data = data;
        }
        else
        {
            if (null == this.next)
            {
                this.next = new Node(data);
            }
            else
            {
                this.next.add(data);
            }
        }
        size += 1;
        return true;
    }

    public Integer getDataAt(int index)
    {
        if (index == 0)
        {
            return this.data;
        }
        else
        {
            return this.next.getDataAt(index - 1);
        }
    }

    public int getSize()
    {
        return size;
    }
}

Please suggest me to edit as of now add(data) to make it O(1) complexity.

5
  • If you had a variable referencing the end of your linked list, then you could add something straight onto it without having to iterate through the list. Commented May 25, 2017 at 11:32
  • Addition to the end or in the beginning is always O(1) since you iterate nothing (if you have reference to the tail), remove operation should be O(n) in default linked list since at most you will need to travers from the end to the beginning. You can always use binary search to remove element (still not O(1) and in need of use double linked list) or use dictionary for it (however it is not true linked list) Commented May 25, 2017 at 11:32
  • @MaLiN2223 You cannot use binary search on linked list. Binary search relies on jumping from index x to index x / 2. In case of linked list, you cannot just "jump", you have to iterate through x / 2 elements. Commented May 25, 2017 at 11:38
  • @JaroslawPawlak what do you mean by "I cannot" ? Of course I can, does anything prevents me from iterating over list even if it is x/2 elements? Commented May 25, 2017 at 11:39
  • @MaLiN2223 If you iterate over your entire list, it's not really a binary search anymore. Binary search has time complexity O(logn), while your pseudo binary search would have time complexity O(n). Commented May 26, 2017 at 9:29

3 Answers 3

5

Only Adding and Removing operation in LinkedList is O(1) but traversing to the node you want to remove or add is an O(N) operation

You can achieve the O(1) complexity if you keep the reference to your last added element so you can put add new Node to the last traversed element's next Node.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, O(1) complexity can be achieved when you add elements in the beginning.
@MaLiN2223 or at the end.
1

In linkedList if you have head and tail pointer to point first and last of node linkedlist then in constant time you can add and remove in first or last position of the node.If you want to delete an element you have to find that element and in worst case that element will be in last .In doubly linkedlist you can start from start and end so you have to traverse till so in worst case it will be O(n).

Comments

0

Thank you for all your support, as a NOOB in data structure I want to understand how ds workds rather than copy pasting from someone's else implementation.

Neeraj Jain & Gati Sahu's explanations/answer helped me to achieve what I'm looking for add(data) in LinkedList with O(1) complexity.

So what I did is "Segregate Plain Node class and create LinkedList class with operations.

class Node
{
    private Integer data    = null;
    private Node    next    = null;

    public Node(Integer data)
    {
        super();
        this.data = data;
    }

    public Integer getData()
    {
        return data;
    }

    public Node getNext()
    {
        return next;
    }

    public void setData(Integer data)
    {
        this.data = data;
    }

    public void setNext(Node next)
    {
        this.next = next;
    }

}

public class LinkedList
{
    Node    head;
    Node    end;

    public Node getHead()
    {
        return head;
    }

    public boolean add(Integer data)
    {
        if (null == head)
        {
            head = new Node(data);
            end = head;
        }
        else
        {
            addAtEnd(data);
        }
        return true;
    }

    public void addAtEnd(Integer data)
    {
        end.setNext(new Node(data));
        end = end.getNext();
    }

    public void addAtFirst(Integer data)
    {
        Node tmpNode = head;
        head = new Node(data);
        head.setNext(tmpNode);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.