问题
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
回答1:
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>
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/18679248/linked-list-time-complexity-for-its-operations