Scenario
We have a linked list containing some elements and we need to build a string of the form [3,6,4,2,4]. If the list is empty, it should output [].
Aim
To write code in Java for traversing the linked list.
Steps for Completion
- Write a toString() method in the LinkedList class as follows:
public String toString() {
}
- Use a while loop to traverse the linked list.
In this section, we have seen how we can implement the various operations found in the linked list. The data structure will be a base tool that we will use to model queues and stacks. Linked lists will also be extensively used in more advanced algorithms further along the book.