π Searching, βοΈ Updating, π’ Counting Nodes & π Reversing a Doubly Linked List
Welcome to Part 4 of our Doubly Linked List in Java series.
So far, we have learned:
β Part 1A
- Introduction to Doubly Linked List
- Need for Doubly Linked List
- Node Structure
- Memory Representation
β Part 1B
- Creating a Doubly Linked List
- Forward Traversal
- Backward Traversal
β Part 2
- Insertion at Beginning
- Insertion at End
- Insertion at Specific Position
β Part 3
- Deletion from Beginning
- Deletion from End
- Deletion at Position
- Deletion by Value
Now we’ll learn four more important operations that complete the core functionality of a Doubly Linked List:
- π Searching
- βοΈ Updating
- π’ Counting Nodes
- π Reversing a Doubly Linked List
These operations are frequently asked in university exams and coding interviews.
Let’s begin!
π Table of Contents
- Searching
- Updating
- Counting Nodes
- Reversing a Doubly Linked List
- Java Programs
- Dry Runs
- Time Complexity
- Common Mistakes
- FAQs
- Conclusion
π Searching in a Doubly Linked List
Searching means finding whether a particular value exists in the linked list.
Suppose we have
10 β 20 β 30 β 40 β 50
Search
30
Result
Element Found
Search
70
Result
Element Not Found
π Algorithm
- Start from Head.
- Compare current node with the required value.
- If found, stop.
- Otherwise move to the next node.
- Continue until NULL.
π» Java Program β Searching
public boolean search(int value){
Node temp = head;
while(temp != null){
if(temp.data == value)
return true;
temp = temp.next;
}
return false;
}
Example
if(list.search(30))
System.out.println("Element Found");
else
System.out.println("Element Not Found");
Output
Element Found
π Dry Run
Current List
10 β 20 β 30 β 40
Search
30
Compare
10 β 30
β
20 β 30
β
30 = 30
Found.
π Time Complexity
Searching requires traversal.
O(n)
Extra Space
O(1)
βοΈ Updating a Node
Updating means replacing an existing value with another value.
Suppose
10 β 20 β 30 β 40
Replace
30
with
35
Result
10 β 20 β 35 β 40
π Algorithm
- Traverse the list.
- Find the required node.
- Replace its data.
- Stop.
π» Java Program β Update
public void update(int oldValue,int newValue){
Node temp = head;
while(temp != null){
if(temp.data == oldValue){
temp.data = newValue;
return;
}
temp = temp.next;
}
System.out.println("Value Not Found");
}
Output
10 β 20 β 35 β 40
π Time Complexity
Traversal required.
O(n)
π’ Counting the Number of Nodes
Sometimes we need to know the size of the linked list.
Suppose
10 β 20 β 30 β 40 β 50
Total Nodes
5
π Algorithm
- Start from Head.
- Initialize count = 0.
- Visit every node.
- Increment count.
- Return count.
π» Java Program β Count Nodes
public int countNodes(){
Node temp = head;
int count = 0;
while(temp != null){
count++;
temp = temp.next;
}
return count;
}
Output
5
π Time Complexity
O(n)
Space
O(1)
π Reversing a Doubly Linked List
One of the most interesting interview questions.
Suppose
10 β 20 β 30 β 40
After reversing
40 β 30 β 20 β 10
Unlike a Singly Linked List,
we must reverse both previous and next references.
π€ How Does Reversal Work?
Every node stores
Previous
Next
To reverse,
we simply swap these two references for every node.
π¨ Visual Explanation
Current
NULL β 10 β 20 β 30 β 40 β NULL
Node 20
Initially
Prev = 10
Next = 30
After swapping
Prev = 30
Next = 10
Every node performs the same operation.
Finally,
Head becomes the old Tail.
π Algorithm
- Start from Head.
- Swap previous and next references.
- Move to the next node (which becomes previous after swapping).
- Continue until the end.
- Update Head.
π» Java Program β Reverse
public void reverse(){
Node current = head;
Node temp = null;
while(current != null){
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if(temp != null)
head = temp.prev;
}
Example
Before
10 β 20 β 30 β 40
After
40 β 30 β 20 β 10
π Dry Run
Initially
10 β 20 β 30 β 40
At Node 10
Swap
Prev = 20
Next = NULL
Move to Node 20.
Repeat.
Eventually
40 β 30 β 20 β 10
π» Complete Java Program
class Node{
int data;
Node prev;
Node next;
Node(int data){
this.data = data;
prev = null;
next = null;
}
}
public class DoublyLinkedList{
Node head;
public void insertAtEnd(int data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
return;
}
Node temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = newNode;
newNode.prev = temp;
}
public boolean search(int value){
Node temp = head;
while(temp != null){
if(temp.data == value)
return true;
temp = temp.next;
}
return false;
}
public void update(int oldValue,int newValue){
Node temp = head;
while(temp != null){
if(temp.data == oldValue){
temp.data = newValue;
return;
}
temp = temp.next;
}
}
public int countNodes(){
Node temp = head;
int count = 0;
while(temp != null){
count++;
temp = temp.next;
}
return count;
}
public void reverse(){
Node current = head;
Node temp = null;
while(current != null){
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if(temp != null)
head = temp.prev;
}
public void display(){
Node temp = head;
while(temp != null){
System.out.print(temp.data + " β ");
temp = temp.next;
}
System.out.println("NULL");
}
public static void main(String args[]){
DoublyLinkedList list = new DoublyLinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.display();
System.out.println(list.search(30));
list.update(30,35);
list.display();
System.out.println("Total Nodes = " + list.countNodes());
list.reverse();
list.display();
}
}
π Time Complexity
| Operation | Time Complexity |
|---|---|
| Search | O(n) |
| Update | O(n) |
| Count Nodes | O(n) |
| Reverse | O(n) |
πΎ Space Complexity
All operations use only a few temporary references.
O(1)
β οΈ Common Mistakes
β Forgetting to swap both references
Many beginners only change the next reference.
In a Doubly Linked List,
both
prev
next
must be swapped.
β Forgetting to update Head
After reversing,
the old Tail becomes the new Head.
Always update
head = temp.prev;
β Traversing incorrectly after swapping
After swapping,
the next node becomes
current.prev
not
current.next
β Frequently Asked Questions
Q1. Why is searching O(n)?
Because we may need to visit every node.
Q2. Can we update a node without searching?
No.
We must first locate the node.
Q3. Why do we swap both prev and next while reversing?
Because both links define the structure of a Doubly Linked List.
Q4. Is reversing done in-place?
Yes.
No additional linked list is created.
Q5. What is the time complexity of reversing?
Every node is visited exactly once.
O(n)
π― Key Takeaways
- π Searching traverses the list from Head.
- βοΈ Updating changes the data stored in a node.
- π’ Counting requires visiting every node.
- π Reversing swaps both
prevandnextreferences. - π All operations require O(n) time.
- πΎ Extra space required is O(1).
π Conclusion
Searching, updating, counting, and reversing complete the set of core operations for a Doubly Linked List. These operations demonstrate the true advantage of storing both previous and next references, enabling efficient bidirectional navigation while keeping memory usage reasonable.
By now, you’ve learned how to create, traverse, insert, delete, search, update, count, and reverse a Doubly Linked List in Java.
π In Part 5 (Final Part), we’ll complete this series by covering:
- π Time Complexity Summary
- βοΈ Singly vs Doubly Linked List
- π Arrays vs Doubly Linked List
- π Real-World Applications
- π― Interview Questions
- π§ͺ Practice Programs
- π Revision Notes
- π Final Conclusion
This final part will serve as your complete revision guide for exams, interviews, and practical programming.