0% found this document useful (0 votes)
5 views3 pages

Java LinkedList Methods Overview

The document provides an overview of the LinkedList class in the java.util package, detailing its features, methods for adding, accessing, removing, and searching elements, as well as iterating over the list. It highlights the LinkedList's ability to maintain insertion order, allow duplicates, and function as a List, Stack, Queue, and Deque. Additionally, it mentions performance characteristics, such as efficient insertions and deletions at the start and end, but slower random access.

Uploaded by

johnbhai7765
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Java LinkedList Methods Overview

The document provides an overview of the LinkedList class in the java.util package, detailing its features, methods for adding, accessing, removing, and searching elements, as well as iterating over the list. It highlights the LinkedList's ability to maintain insertion order, allow duplicates, and function as a List, Stack, Queue, and Deque. Additionally, it mentions performance characteristics, such as efficient insertions and deletions at the start and end, but slower random access.

Uploaded by

johnbhai7765
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA PRINT WALL — LINKEDLIST ([Link].

LinkedList)

🔹 1. Introduction - Part of [Link] package. - Implements: List, Deque, Queue, Serializable, Cloneable. -
Doubly linked list internally. - Maintains insertion order. - Allows duplicates and null elements. - Dynamic
memory allocation (size can grow/shrink).

🔹 2. Declaration & Creation

LinkedList<String> list = new LinkedList<>();


LinkedList<Integer> numbers = new LinkedList<>([Link](1,2,3));

- Can be empty or initialized with another collection.

🔹 3. Adding Elements | Method | Purpose | |--------|--------| | add(E e) | Adds element at end | |


add(int index, E element) | Adds element at specific index | | addFirst(E e) | Adds element at start | |
addLast(E e) | Adds element at end | | offer(E e) | Adds element at end (Queue style) | | offerFirst(E e) |
Adds element at front | | offerLast(E e) | Adds element at end |

Example:

LinkedList<String> list = new LinkedList<>();


[Link]("A");
[Link]("Start");
[Link]("End");
[Link](list);

Output: [Start, A, End]

🔹 4. Accessing Elements | Method | Purpose | |--------|--------| | get(int index) | Element at index | |


getFirst() | First element | | getLast() | Last element | | peek() | Retrieves first element (null if empty) |
| peekFirst() | First element without removing | | peekLast() | Last element without removing |

Example:

[Link]([Link](1)); // A
[Link]([Link]()); // Start
[Link]([Link]()); // End

🔹 5. Removing Elements | Method | Purpose | |--------|--------| | remove() | Removes first element | |


remove(int index) | Removes element at index | | remove(Object o) | Removes first occurrence | |
removeFirst() | Removes first element | | removeLast() | Removes last element | | poll() | Retrieves &
removes first element | | pollFirst() | Retrieves & removes first element | | pollLast() | Retrieves &
removes last element | | clear() | Removes all elements |

Example:

1
[Link]();
[Link]();
[Link](list);

Output: [A]

🔹 6. Searching Elements | Method | Purpose | |--------|--------| | contains(Object o) | Checks if element


exists | | indexOf(Object o) | First occurrence index | | lastIndexOf(Object o) | Last occurrence index |

Example:

[Link]([Link]("A")); // true
[Link]([Link]("A")); // 0

🔹 7. Iterating Over LinkedList - Using For Loop:

for(int i=0; i<[Link](); i++) [Link]([Link](i)+" ");

- Using Enhanced For Loop:

for(String s : list) [Link](s+" ");

- Using Iterator:

Iterator<String> it = [Link]();
while([Link]()) [Link]([Link]()+" ");

- Using ListIterator (Forward & Backward):

ListIterator<String> lit = [Link]();


while([Link]()) [Link]([Link]()+" ");
while([Link]()) [Link]([Link]()+" ");

🔹 8. Stack-like Operations | Method | Purpose | |--------|--------| | push(E e) | Adds element at head


(Stack push) | | pop() | Removes & returns head (Stack pop) | | peek() | Returns head without removing
|

Example:

[Link]("X");
[Link]([Link]());

Output: X

2
🔹 9. Queue-like Operations | Method | Purpose | |--------|--------| | offer(E e) | Adds element at end | |
poll() | Removes & returns head | | peek() | Returns head without removing |

Example:

[Link]("Y");
[Link]([Link]());

Output: A

🔹 10. Conversion to Array

Object[] arr = [Link]();


String[] strArr = [Link](new String[0]);

🔹 11. Cloning

LinkedList<String> cloneList = (LinkedList<String>) [Link]();


[Link](cloneList);

🔹 12. Summary of Features - Maintains insertion order - Allows duplicates - Can be used as List, Stack,
Queue, Deque - Efficient insert/delete at start/end (O(1)) - Slow random access (O(n) for get(index))

Common questions

Powered by AI

LinkedList is inefficient for indexed random access with O(n) complexity, as each access requires traversal from the head to the desired index. In contrast, sequential access through iterators or loops is more efficient, as each element is visited in sequence, minimizing unnecessary traversal .

LinkedList offers efficient insertion and deletion at the start and end of the list with O(1) time complexity because of its underlying doubly linked list structure . However, accessing elements by index is slower with O(n) time complexity, as it requires traversal from the head node to the desired index .

LinkedList provides convenient methods for stack and queue-like operations, such as push and pop for stack operations, and offer, poll, and peek for queue operations. These methods leverage the underlying doubly linked list for efficient O(1) operations at the head (for stack) and tail (for queue).

LinkedList provides methods such as getFirst, getLast, removeFirst, and removeLast for accessing and removing first and last elements efficiently. These methods are useful in various scenarios, including implementing data structures like deques or managing head and tail operations quickly .

LinkedList can be iterated using a basic for loop with index-based access, enhanced for loop, Iterator, and ListIterator. Using index-based access can be inefficient with O(n^2) complexity for large lists due to repeated traversal. Enhanced for loops and Iterators generally offer more efficient iteration by providing direct access to elements .

LinkedList supports dynamic memory allocation by allowing its size to grow and shrink dynamically as elements are added or removed. Unlike arrays, its memory allocation is not fixed at creation, providing flexibility in memory usage .

The offer methods (offer, offerFirst, offerLast) add elements to the LinkedList in a queue-style manner, appending at the end or front of the list. These can simulate queue operations where order preservation is required, such as task scheduling or buffering processes .

The addFirst method adds an element to the start of a LinkedList, while addLast adds an element to the end. Both operations directly manipulate the linked list structure. AddFirst may be preferable for stack-like behavior, while addLast supports queue-like behavior .

Yes, a LinkedList can contain null elements as it allows duplicates and null values . The presence of null elements may complicate certain operations, such as iteration and search, particularly if methods do not account for null condition checks.

Cloning a LinkedList creates a shallow copy of the LinkedList, where the elements themselves are not cloned, but the list structure is duplicated. This is accomplished via the clone method, which results in a new LinkedList with the same elements .

You might also like