Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linked List time complexity for its operations [duplicate]

I am implementing a Linked list in terms of stock market program.

It has and operation - Buy

For Buy the code is

//Stocks is a linked List like so 
//LinkedList<Integer> stocks = new LinkedList<Integer>();
public void buy(int q, int p) {
    stocks.addLast(q); //add number of stocks
    stocks.addLast(p); //for i stocks i +1 = price of stock
}

This operation addLast is for a Linked list obvious adds the given element to a new position at the end of a current list.

So for example if I have a list that has lets say the following data

//Stock, price, stock, price etc...
[100, 50, 5000, 30, 8000, 60]

If I addLast is the Linked List search for the last element and then adding and therefore the time complexity would be O(n) (In terms of Big Oh only). Or is it indexing to the end of the list, realizing that the end of the list is say stocks[5] then inserting a new node referencing the new data at the end of the list?

So my question is, is addLast() operation for a linked list time complexity of O(n) or O(1)?

Post below for any clarifications

like image 540
Jim Avatar asked Sep 08 '13 00:09

Jim


People also ask

Does LinkedList allow duplicate values?

Each element is stored as a node. The LinkedList can have duplicate elements because of each value store as a node. But there may be a situation when we want to store only unique elements in LinkedList and want to remove duplicates from linked list. We will discuss some ways that can remove duplicates from linked list.

What is the time complexity of linked list?

In a singly linked list, the time complexity for inserting and deleting an element from the list is O(n). In a doubly-linked list, the time complexity for inserting and deleting an element is O(1).

How do you duplicate a linked list?

Create a duplicate (say Y) for each node (say X) and map them with corresponding old nodes (say mp, So mp[X] = Y). Create the single linked list of the duplicate nodes where each node only has the 'next' pointer.

What is the time complexity for retrieval of data from a linked list?

Time Complexity of Linked List. From this article on time complexity of memory address, we known that to access a specific element, the time complexity is O(√N) where N is block of continuous elements being read. As Linked List elements are not contiguous, each element access incur a Time Complexity of O(√N).


3 Answers

If you read the javadoc for the LinkedList class, it states: "All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index."

This means that it is O(1)

Edit

If you want a better implementation of your Stock name, price list, I think it would be better to create a class containing the description of the stocks:

public class Stock {
    private int stockName;
    private int stockPrice;

    // other methods, properties, constructor
}

And then create a LinkedList<Stock>

like image 105
nasser-sh Avatar answered Sep 20 '22 23:09

nasser-sh


The complexity of add() or addLast() is O(1) on the length of the list. This is obvious from reading the LinkedList source code.

(Since this aspect of the behaviour is not specified precisely in the javadoc1, it could be changed ... in theory. However, we can confidently exclude this possibility. Even if it made sense (which it doesn't!), such a change would break many, many existing applications. That alone is sufficient reason to rule it out as implausible.)


1 - I would argue that the javadoc sentence "[o]perations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index" does not unambiguously apply to this method. You could argue that the add / addLast method does not index into the list.

like image 33
Stephen C Avatar answered Sep 20 '22 23:09

Stephen C


Looking at the source for the LinkedList class it appears that addLast actually calls a method called linkLast which is:

void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

The relevant portion is where last is declared in the class transient Node<E> last; it appears that the class holds a "pointer" to the last node. It then updates with the new node given. So, it should be O(1) regardless of the size of the list.

like image 23
claydiffrient Avatar answered Sep 18 '22 23:09

claydiffrient