LinkedList Methods Flashcards

1
Q

Declaring a LinkedList

A

LinkedList linkedlist = new LinkedList();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Adding an Item at first of the LinkedList

A

Method 1 linkedlist.addfirst(“First-Element”);

Method 2 linkedlist.push(“New-Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Adding an Item at Last of the LinkedList

A

Method 1 linkedlist.addlast(“Last-Element”);

Method 2 linkedlist.add(“Last-Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Adding an element at a particular position of LinkedList

A

linkedlist.add(3,Element);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Removing the first Item from the LinkedList

A

Method 1 linkedlist.removefirst();
Method 2 linkedlist.pop();
Method 3 linkedlist.poll();
Method 4 linkedlist.pollfirst();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Removing the last Item from the LinkedList

A

Method 1 linkedlist.removelast();

Method 2 linkedlist.polllast();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Removing an Item from the LinkedList (Any Element)

A

linkedlist.remove(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Removing an Item from a particular position of an LinkedList

A

linkedlist.remove(2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Retrieving an Item from an LinkedList based on the Index

A

Object X = linkedlist.get(5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Retrieving first Item from an LinkedList

A

Method 1 Object FirstElement = linkedlist.getfirst();
Method 2 Object FirstElement = linkedlist.peek();
Method 3 Object FirstElement = linkedlist.peekfirst();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Retrieving last Item from an LinkedList

A

Method 1 Object LastElement = linkedlist.getlast();

Method 2 Object LastElement = linkedlist.peeklast();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Retrieving first occurrence of Item from an LinkedList - Returns Index

A

linkedlist.indexof(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Retrieving last occurrence of Item from an LinkedList - Returns Index

A

linkedlist.lastindexof(“Element”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Updating the value of the element at a particular index in an LinkedList

A

linkedlist.set(2,”New-Value-Of-The-Element”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Size of an LinkedList

A

linkedlist.size();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Duplicate a linkedlist to another linkedlist

A

DuplicateLinkedlist = linkedlist.clone();

17
Q

Iterating over an LinkedList (Forward) (Method 1: For Loop)

A

Method 1 - FOR LOOP
for (int counter = 0; counter < linkedlist.size(); counter++) {
System.out.println(arraylist.get(counter));

18
Q

Iterating over an LinkedList (Forward) (Method 2: While Loop)

A
Method 2 - WHILE LOOP 
int count = 0; 		
while (linkedlist.size() > count) {
    System.out.println(arraylist.get(count));
    count++;
     }
19
Q

Iterating over an LinkedList (Forward) (Method 3: ITERATOR )

A

Method 3 - ITERATOR
Iterator iterator = linkedlist.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

20
Q

Iterating over an LinkedList (Backward)

A

BACKWARD ITERATING
ListIterator iterator = linkedlist.iterator();
while (iterator.hasPrevious()) {
System.out.println(iterator.previous());

21
Q

Is this Item there in the LinkedList

A

linkedlist.contains(“Element”);

22
Q

Difference Between ArrayList And LinkedList

A

ArrayList and LinkedList both implements List interface and their methods and results are almost identical. However there are few differences between them which make one better over another depending on the requirement. ArrayList Vs LinkedList

Search: ArrayList search operation is pretty fast compared to the LinkedList search operation. get(int index) in ArrayList gives the performance of O(1) while LinkedList performance is O(n).Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.
Deletion: LinkedList remove operation gives O(1) performance while ArrayList gives variable performance: O(n) in worst case (while removing first element) and O(1) in best case (While removing last element).Conclusion: LinkedList element deletion is faster compared to ArrayList.Reason: LinkedList’s each element maintains two pointers (addresses) which points to the both neighbor elements in the list. Hence removal only requires change in the pointer location in the two neighbor nodes (elements) of the node which is going to be removed. While In ArrayList all the elements need to be shifted to fill out the space created by removed element.
Inserts Performance: LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. Reason is same as explained for remove.
Memory Overhead: ArrayList maintains indexes and element data while LinkedList maintains element data and two pointers for neighbor nodes hence the memory consumption is high in LinkedList comparatively.
23
Q

Similarities between ArrayList and LinkedList

A

Both ArrayList and LinkedList are implementation of List interface.
They both maintain the elements insertion order which means while displaying ArrayList and LinkedList elements the result set would be having the same order in which the elements got inserted into the List.
Both these classes are non-synchronized and can be made synchronized explicitly by using Collections.synchronizedList method.
The iterator and listIterator returned by these classes are fail-fast (if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException).