Hello there! Today, we will explore Linked Lists, a core data structure crucial for organized data management and establishing relationships between data.
We will mark some essential milestones: an introduction to Linked Lists, their real-world applications, their implementation in Java, and the different operations you can perform on them.
By the end of this lesson, you will be well-equipped to implement and operate Linked Lists in Java. Let's get started!
A Linked List is a linear data structure similar to arrays. But, unlike arrays, they are not stored in contiguous memory locations. Each element in a Linked List is part of a node
. A node
comprises data and a reference (or link) to the next node
in the sequence. This structure facilitates efficient insertions and deletions.
The head
is also an essential concept in Linked Lists. It is the first node
in the list and a reference to the entire list. The head
is a null reference if the Linked List is empty.
Singly linked lists come up quite often in coding interviews. Interviewers from tech giants, start-ups, and just about every company testing your coding abilities will pose challenges based on this concept.
Here's another interesting point: While singly linked lists might not be extensively used in real-world applications, they form the foundational knowledge for understanding doubly linked lists, which are indeed quite common.
To begin implementing Linked Lists, we first need to understand the structure of a node
, the building block of a Linked List. Regarding Java code, we need to create a class acting as a blueprint for a node
.
A Node
class mainly consists of data
(the data you want to store) and next
(the reference to the next node
). In our case, we'll create a Node class to store integer data.
Java1class Node { 2 int data; 3 Node next; 4}
Fantastic! You now know how to set up a node
in a Linked List.
In this section, we'll learn how to add a new node at the end of our Linked List. We'll implement an append
function in our LinkedList class for this.
Java1class Node { 2 int data; 3 Node next; 4} 5 6public class LinkedList { 7 Node head; 8 9 public void append(int data) { 10 Node node = new Node(); 11 node.data = data; 12 node.next = null; 13 14 if (head == null) { 15 head = node; 16 } else { 17 Node last = head; 18 while (last.next != null) { 19 last = last.next; 20 } 21 last.next = node; 22 } 23 } 24}
The code checks if head
is null
, which would be the case for an empty list. If that's true, head
is set to the new node, meaning this new node is the first and only node in the list. If the linked list is not empty (head
is not null
), the code enters a while loop, which runs as long as the next
attribute of the current node it's looking at is not null
(i.e., there are still nodes in the list). This loop is used to navigate to the end of the list. The new node is then appended after the last node in the list.
Now, what if we want to add a new node at the beginning of our list? We'll write a function addFirst
to achieve this operation.
Java1public void addFirst(int data) { 2 Node node = new Node(); 3 node.data = data; 4 node.next = null; 5 6 if (head != null) { 7 node.next = head; 8 } 9 head = node; 10 }
It simply reassigns the head.
Removing a node from a Linked List is also an essential functionality. We will add a delete
function to our LinkedList class to remove a node with a particular data value.
Java1public void delete(int data) { 2 if (head == null) return; 3 4 if (head.data == data) { 5 head = head.next; 6 return; 7 } 8 9 Node current = head; 10 while (current.next != null) { 11 if (current.next.data == data) { 12 current.next = current.next.next; 13 return; 14 } 15 current = current.next; 16 }
We traverse the list like in the append
operation, searching for a node with specific data. If found, it is removed from the list by retargeting the previous node to the node after the target.
While understanding the implementation of Linked Lists is great, it's equally crucial to comprehend the performance of our operations. This understanding generally comes through complexity analysis, examining the time (number of operations) and space (memory used) needed for an operation.
Here's a summary of the performance of particular operations in Linked Lists:
Great job sticking with it throughout this intriguing journey, from understanding the concept of Linked Lists to implementing them in Java, exploring critical operations, and understanding their performance!
Up ahead, we have lined up some practice sessions. These sessions will provide you with real-time experience implementing and manipulating Linked Lists in Java.