LinkedLists Java NPTEL Notes
LinkedLists Java NPTEL Notes
1. Basic Concepts:
- Array: Stores elements in contiguous memory locations and has static size (fixed data).
- Linked List: Uses dynamic memory allocation and is linear. It's implemented using nodes and
pointers, where each node contains the data and a reference (pointer) to the next node.
- Singly Linked List: Each node points to the next node, and the last node points to null.
- Doubly Linked List: Each node contains references to both the next and previous nodes.
- Circular Linked List: The last node points back to the first node, creating a circular chain.
- Insertion:
At the beginning:
```
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
newNode.next = head;
return newNode;
```
- Deletion:
At the beginning:
```
return head.next;
```
- Traversal:
```
current = current.next;
```
- Reversing:
```
next = current.next;
current.next = prev;
prev = current;
current = next;
return prev;
```
```
class DoublyNode {
int data;
DoublyNode(int data) {
this.data = data;
newNode.next = head;
return newNode;
```
- Sparse Matrix Multiplication: Linked lists can represent sparse matrices efficiently by only storing
non-zero elements.
- Polynomial Manipulation: Linked lists store polynomials, where each node represents a term,
- Memory Management: Linked lists can be used to manage free memory blocks in dynamic
The LinkedList class in Java is part of the Java Collections Framework (JCF). It supports sequential
access to elements and inherits from AbstractSequentialList. It implements both List and Deque
interfaces, providing methods to add, remove, and manipulate elements at both ends.
- Linked-List Data Structure: Provides a linked-list data structure where elements are stored
sequentially.
- Doubly Linked List: Uses a doubly linked list internally to store the elements.
- Maintains Insertion Order: Maintains the order in which elements are inserted.
- Non-Synchronized: The class is not synchronized by default, meaning it is not thread-safe unless
explicitly synchronized.
- Faster Manipulation: No shifting is required, so insertion and deletion are faster than in arrays.
- Versatility: The class can be used to maintain a collection as a linked list, stack, or queue.