ArrayList Vs LinkedList

Remember that big-O complexity describes asymptotic behaviour and may not reflect actual implementation speed. It describes how the cost of each operation grows with the size of the list, not the speed of each operation. For example, the following implementation of add is O(1) but is not fast: public class MyList extends LinkedList { public … Read more

Where can I see the source code of the Sun JDK?

Install the Java SE Development Kit from http://java.sun.com/javase/downloads/index.jsp. Once installed, you should find an archive called src.zip in the top of the JDK installation directory. The Java source code is in there. The file is java/util/LinkedList.java. update: You may also like to visit the online OpenJDK Source repository. See this answer below.

Binary Trees vs. Linked Lists vs. Hash Tables

The standard trade offs between these data structures apply. Binary Trees medium complexity to implement (assuming you can’t get them from a library) inserts are O(logN) lookups are O(logN) Linked lists (unsorted) low complexity to implement inserts are O(1) lookups are O(N) Hash tables high complexity to implement inserts are O(1) on average lookups are … Read more

Creating a very simple linked list

A Linked List, at its core is a bunch of Nodes linked together. So, you need to start with a simple Node class: public class Node { public Node next; public Object data; } Then your linked list will have as a member one node representing the head (start) of the list: public class LinkedList … Read more

What is the reason for using a double pointer when adding a node in a linked list?

Some implementations pass a pointer to pointer parameter to allow changing the head pointer directly instead of returning the new one. Thus you could write: // note that there’s no return value: it’s not needed void push(struct node** head, int data) { struct node* newnode = malloc(sizeof(struct node)); newnode->data=data; newnode->next=*head; *head = newnode; // *head … Read more

Why increase pointer by two while finding loop in linked list, why not 3,4,5?

From a correctness perspective, there is no reason that you need to use the number two. Any choice of step size will work (except for one, of course). However, choosing a step of size two maximizes efficiency. To see this, let’s take a look at why Floyd’s algorithm works in the first place. The idea … Read more

Why does cache locality matter for array performance?

See my answer about spatial and temporal locality. In particular, arrays are contiguous memory blocks, so large chunks of them will be loaded into the cache upon first access. This makes it comparatively quick to access future elements of the array. Linked lists on the other hand aren’t necessarily in contiguous blocks of memory, and … Read more