Tutorial Playlist
191 Lessons1. Introduction to Java
2. What is Java?
3. History of Java
4. Java Tutorial for Beginners
5. How Do Java Programs Work?
6. JDK in Java
7. C++ Vs Java
8. Java vs. Python
9. Java vs. JavaScript
10. From Java Source Code to Executable
11. How to Install Java in Linux
12. How to Install Java in Windows 10
13. Java Hello World Program
14. Structure of Java Program and Java Syntax
15. Operators in Java
16. Java If-else
17. Switch Case In Java
18. Loops in Java
19. Infinite loop in Java
20. For Loop in Java
21. For Each Loop in Java
22. Constructor in Java
23. Constructor Overloading in Java
24. Copy Constructor in Java
25. Default Constructor in Java
26. Parameterized Constructors in Java
27. Constructor Chaining In Java
28. Finalize Method in Java
29. Static Method in Java
30. Equals Method in Java
31. Abstract Method in Java
32. toString() Method in Java
33. Difference between equals method in Java
34. Inheritance in Java
35. Multiple Inheritance in Java
36. Hierarchical Inheritance in Java
37. Java Classes and Objects
38. Scanner Class in java
39. All classes in java are inherited from which class
40. What is Nested Class in Java
41. POJO Class in Java
42. Anonymous Class in Java
43. Final Class in Java
44. Object Class in Java
45. Packages in Java
46. Access Modifiers in Java
47. Static Keyword In Java
48. Final Keyword in Java
49. Checked and Unchecked Exceptions in Java
50. User Defined Exception in Java
51. Error vs. Exception in Java
52. Java Collection
53. Collections in Java
54. Garbage Collection in Java
55. Generics In Java
56. Java Interfaces
57. Functional Interface in Java
58. Marker Interface in Java
59. Streams in Java
60. Byte stream in java
61. File Handling in Java
62. Thread in Java
63. Thread Lifecycle In Java
64. Daemon Thread in Java
65. Thread Priority in Java
66. Deadlock in Java
67. String Pool in Java
68. Java Database Connectivity(JDBC)
69. Design Patterns in Java
70. Functional Programming in Java
71. OOP vs Functional vs Procedural
72. Heap Memory and Stack Memory in Java
73. Applet in Java
74. Java Swing
75. Java Frameworks
76. Hibernate Framework
77. JUnit Testing
78. How to Install Eclipse IDE for Java?
79. Command line arguments in Java
80. Jar file in Java
81. Java Clean Code
82. OOPs Concepts in Java
83. Java OOPs Concepts
84. Overloading vs Overriding in Java
85. Java 8 features
86. String in Java
87. String to int in Java
88. Why String Is Immutable in Java?
89. Primitive Data Types in Java
90. Non-Primitive Data Types in Java
91. This and Super Keyword in Java
92. HashMap in Java
93. Comparable And Comparator in Java
94. Type Casting in Java
95. Arrays Sort in Java with Examples
96. Variable Hiding and Variable Shadowing in Java
97. Enum in Java
98. Substring in Java
99. Pattern Programs in Java
100. Hashcode in Java
101. What is ByteCode in Java?
102. How To Take Input From User in Java
103. GCD of Two Numbers in Java
104. Linked List in Java
Now Reading
105. Arithmetic Operators in Java
106. Conditional Operators in Java
107. Stack and Queue in Java
108. Array Length in Java
109. Number Pattern Program in Java
110. Split in java
111. Map In Java
112. Difference Between Throw and Throws in Java
113. Difference Between Data Hiding and Abstraction
114. HashSet in Java
115. String Length in Java
116. Factorial Using Recursion in Java
117. DateFormat in Java
118. StringBuilder Class in java
119. Instance variables in Java
120. Java List Size
121. Java APIs
122. Reverse an Array in Java
123. StringBuffer and StringBuilder Difference in Java
124. Java Program to Add Two Numbers
125. String to Array in Java
126. Regular Expressions in Java
127. Identifiers in Java
128. Data Structures in Java
129. Set in Java
130. Pass By Value and Call By Reference in Java
131. Try Catch in Java
132. Bubble Sort in Java
133. Caesar Cipher Program in Java
134. Queue in Java
135. Object Creation in Java
136. Multidimensional Array in Java
137. How to Read a File in Java
138. String Comparison in Java
139. Volatile Keyword in Java
140. Control Statements in Java
141. Jagged Array in Java
142. Two-Dimensional Array in Java
143. Java String Format
144. Replace in Java
145. charAt() in Java
146. CompareTo in Java
147. Matrix Multiplication in Java
148. Static Variable in Java
149. Event Handling in Java
150. parseInt in Java
151. Java ArrayList forEach
152. Abstraction in Java
153. String Input in Java
154. Logical Operators in Java
155. instanceof in Java
156. Math Floor in Java
157. Selection Sort Java
158. int to char in Java
159. Stringtokenizer in java
160. Implementing and Manipulating Abs in Java
161. Char array to string in java
162. Convert Double To String In Java
163. Deque in Java
164. Converting a List to an Array in Java
165. The Max function in java
166. Removing whitespace from string in java
167. String arrays in Java
168. Strings in Java Vs Strings in Cpp
169. Sum of digits of a number in Java
170. Art of Graphical User Interfaces
171. Trim in Java
172. RxJava
173. Recursion in Java
174. HashSet Java
175. Difference Between Java and Python
176. Square Root in Java
177. Reverse A String in Java
178. Even Odd Program in Java
179. Fibonacci Series in Java
180. Prime Number Program in Java
181. Java Program to Print Prime Numbers in a Given Range
182. Java Leap Year Program
183. Swapping of Two Numbers in Java
184. LCM of Two Numbers in Java
185. Math.sqrt() Function in Java
186. Area of Triangle in Java
187. Sort a String In Java
188. Factorial Program in Java
189. Javafx
190. Lambda expression in java
191. Setup Java Home and IDE on macOS
A LinkedList in Java is a linear and fundamental data structure that stores the Java components. It offers Java users an efficient, flexible way to store and work with various Java elements.
Java LinkedList allows dynamic size adjustment as and when elements are subtracted or added. Here in this tutorial, you will learn what is LinkedList, its needs, its operation, demonstration, LinkedList in a data structure, and much more.
LinkedList is an element sequence where all the elements are kept safe in nodes. Along with the elements, each node has a reference point directed toward the next node. Read on to learn more about various linked list methods in Java, the insertion and deletion process in a LinkedList in Java, the creation of a LinkedList in Java, and so on.
Linked lists are used because they have a range of advantages. They make better use of the memory from the viewpoint of allocation. The size for this list is also not pre-determined and thus allows easy addition and subtraction of elements from the list. Even for sorting algorithms LinkedLists are more efficient.
To create a linked list in Java, we must first define a class to represent the nodes of the list and implement methods to perform various operations on the list.
Let us understand linked lists with an example of creating a linked list.
We will have two classes: Node and LinkedList. The Node class will represent a single node in the linked list, storing an integer value and a reference to the next node. The LinkedList class will represent the linked list, maintaining a reference to the head node.
The LinkedList class will provide methods to insert nodes at the end of the list (insert method) and display the list's contents (display method).
We will then add the main method to create an instance of the LinkedList class and insert some elements into the list using the insert method. Finally, we display the contents of the list using the display method.
Here is the code:
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
Node head;
public LinkedList() {
this.head = null;
}
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
public void display() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
list.display();
}
}
A doubly linked list is a type of linked list where all the nodes contain references to both the next and previous nodes, allowing for efficient traversal in both directions. In Java, you can implement a doubly linked list using a custom class representing the nodes and maintaining the necessary references.
Here is an example:
Code:
class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
Node head;
Node tail;
public DoublyLinkedList() {
this.head = null;
this.tail = null;
}
// Methods for various operations on the doubly linked list go here
public void addFirst(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
}
public void addLast(int data) {
Node newNode = new Node(data);
if (tail == null) {
head = newNode;
tail = newNode;
} else {
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
}
}
public void insertAfter(Node node, int data) {
if (node == null) {
return;
}
Node newNode = new Node(data);
newNode.next = node.next;
newNode.prev = node;
if (node.next != null) {
node.next.prev = newNode;
}
node.next = newNode;
if (tail == node) {
tail = newNode;
}
}
public void remove(Node node) {
if (node == null) {
return;
}
if (node.prev != null) {
node.prev.next = node.next;
} else {
head = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
} else {
tail = node.prev;
}
}
public void displayForward() {
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public void displayBackward() {
Node current = tail;
while (current != null) {
System.out.print(current.data + " ");
current = current.prev;
}
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
// Adding elements
list.addFirst(10);
list.addLast(20);
Node node = list.head;
list.insertAfter(node, 30);
// Displaying elements
System.out.print("Forward order: ");
list.displayForward();
System.out.print("Backward order: ");
list.displayBackward();
// Updating elements
node = list.head;
node.data = 40;
// Removing elements
node = list.head.next;
list.remove(node);
// Displaying elements after update and removal
System.out.print("Forward order after update and removal: ");
list.displayForward();
}
}
In the above example, we have a Node class representing a single node in the doubly linked list. Each node has an integer data value and references to the previous and next nodes in the list.
The DoublyLinkedList class represents the doubly linked list itself. It maintains references to the head and tail nodes of the list.
The LinkedList class provides several constructors in Java to create LinkedList objects. Here are the commonly used constructors:
Example:
There are several methods to perform various operations on the linked list in Java. Here are some commonly used methods:
Here is an example demonstrating various operations on a LinkedList:
Code:
import java.util.LinkedList;
public class LinkedListOperations {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
// Adding elements
linkedList.add("Apple");
linkedList.add("Banana");
linkedList.add("Orange");
System.out.println("Initial LinkedList: " + linkedList);
// Updating elements
linkedList.set(1, "Mango");
System.out.println("After updating: " + linkedList);
// Removing elements
linkedList.remove("Orange");
linkedList.removeFirst();
System.out.println("After removal: " + linkedList);
// Iterating over elements
System.out.print("Elements in the LinkedList: ");
for (String element : linkedList) {
System.out.print(element + " ");
}
System.out.println();
// Converting to an array
Object[] array = linkedList.toArray();
System.out.print("Array representation: ");
for (Object element : array) {
System.out.print(element + " ");
}
System.out.println();
// Size of the LinkedList
System.out.println("Size of the LinkedList: " + linkedList.size());
// Removing first and last elements
linkedList.removeFirst();
linkedList.removeLast();
System.out.println("After removing first and last: " + linkedList);
}
}
The LinkedList class can also be used as a queue in Java by using the methods provided by the Queue interface. Here's an example of using a LinkedList as a queue:
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements to the queue
queue.add("Apple");
queue.add("Banana");
queue.add("Orange");
// Displaying the queue
System.out.println("Queue: " + queue);
// Removing elements from the queue
String removedElement = queue.remove();
System.out.println("Removed element: " + removedElement);
// Accessing the head of the queue
String head = queue.peek();
System.out.println("Head of the queue: " + head);
// Checking if the queue is empty
boolean isEmpty = queue.isEmpty();
System.out.println("Is the queue empty? " + isEmpty);
}
}
In this example, we create a LinkedList and assign it to a Queue reference. We then use the queue-specific methods such as add, remove, peek, and isEmpty to perform queue operations.
When using LinkedList as a queue, elements are added to the end of the list (add method) and removed from the front of the list (remove method), following the FIFO (First-In-First-Out) order.
The LinkedList class can also be used as a double-ended queue (deque) in Java by utilizing the methods provided by the Deque interface. Here's an example of using a LinkedList as a deque:
import java.util.LinkedList;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> deque = new LinkedList<>();
// Adding elements to the front of the deque
deque.addFirst("Apple");
deque.addFirst("Banana");
// Adding elements to the end of the deque
deque.addLast("Orange");
deque.addLast("Mango");
// Displaying the deque
System.out.println("Deque: " + deque);
// Removing elements from the front and end of the deque
String removedFirst = deque.removeFirst();
String removedLast = deque.removeLast();
System.out.println("Removed elements: " + removedFirst + ", " + removedLast);
// Accessing the first and last elements of the deque
String first = deque.peekFirst();
String last = deque.peekLast();
System.out.println("First element: " + first);
System.out.println("Last element: " + last);
}
}
In this example, we create a LinkedList and assign it to a Deque reference. We then use the deque-specific methods such as addFirst, addLast, removeFirst, removeLast, peekFirst, and peekLast to perform deque operations.
ArrayList and LinkedList are manifestations of the Collection framework’s List interface. But, there are certain minute differences between the two terms.
Here’s a section on LinkedList vs. ArrayList Java to help you understand the key differences:
Here are the various advantages of LinkedList in Java:
Much like vectors in Java, LinkedList is featured with the ability to grow in size or shrink. It simply means that to accommodate the addition of new elements, the LinkedList can grow. Or, it can shrink itself in case any elements are removed.
Compared to arrays, LinkedList has more efficient memory use. Array keeps all the elements in the memory locations, even though some elements might not be useful. LinkedList, however, allocates memory selectively. Memory is allocated only to the elements in use and thus is a memory savior where the data set size is variable or unknown.
When compared to arrays, LinkedLists have more efficient sorting algorithms. Unlike arrays, swapping elements are unnecessary for LinkedLists, saving valuable time.
LinkedList offers efficient element manipulation in Java, which lets you comfortably add or subtract elements. It is a part of the Collection Framework and implements interfaces like Queue, List, and Deque. It has numerous advantages over an array, for being dynamic in size and having memory efficiency, but it also lacks an array’s capability of accessing random elements.
Considering the range of advantages, LinkedLists can be applicable in your day-to-day work. For instance, well-known software like Canva, MS-Word have an ‘undo’ feature executed by LinkedLists. Thus, it is an essential feature of the Java platform that you must master if you want to become a Java expert.
1. Is LinkedList in C the same as LinkedList in Java?
The implementation and syntax of LinkedList in C and Java are different. LinkedLists are not a complementary, built-in structure in C as in Java. In C, pointers and structures are used to implement LinkedLists, while in Java, it is implementable in a doubly linked list.
2. What are the various types of LinkedLists?
The various types of LinkedLists are:
3. What are some LinkedList applications?
LinkedLists can be used for implementing Queues, Stacks. It also helps the user fill in elements at the head or the list's tail.
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...