Given a standard linked list of int, L, write a method to print L in order: 1. using a loop 2. using recursion. start printing from head.next (In simple java language. Please add comments)
Given a standard linked list of int, L, write a method to print L in order: 1. using a loop 2. using recursion. start printing from head.next (In simple java language. Please add comments)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
100%
Code with comments and output screenshot is must. Thank you!
![### Printing a Linked List in Java
Given a standard linked list of integers (`int`), `L`, we want to write two methods to print the elements of `L` in order:
1. Using a loop.
2. Using recursion.
We will start printing from `head.next`.
#### Using a Loop
Below is the Java code to print the elements of a linked list using a loop:
```java
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class LinkedList {
Node head; // head of the list
/* Function to print linked list using loop starting from head.next */
void printListUsingLoop() {
Node temp = head.next; // Start from the next node
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Creating a simple linked list
list.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
list.head.next = second; // Link first node with the second node
second.next = third; // Link second node with the third node
list.printListUsingLoop();
}
}
```
#### Using Recursion
Below is the Java code to print the elements of a linked list using recursion:
```java
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class LinkedList {
Node head; // head of the list
/* Function to print linked list using recursion starting from head.next */
void printListUsingRecursion(Node node) {
if (node == null) {
return; // Base case: end of the list
}
System.out.print(node.data + " ");
printListUsingRecursion(node.next);
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Creating a simple linked list
list.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
list.head.next = second; //](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fc8241d13-b770-4dad-946c-6942db4056c7%2Ff231f3ab-51bc-4518-b7b0-c60bfc13e813%2F6k34yu9_processed.jpeg&w=3840&q=75)
Transcribed Image Text:### Printing a Linked List in Java
Given a standard linked list of integers (`int`), `L`, we want to write two methods to print the elements of `L` in order:
1. Using a loop.
2. Using recursion.
We will start printing from `head.next`.
#### Using a Loop
Below is the Java code to print the elements of a linked list using a loop:
```java
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class LinkedList {
Node head; // head of the list
/* Function to print linked list using loop starting from head.next */
void printListUsingLoop() {
Node temp = head.next; // Start from the next node
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Creating a simple linked list
list.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
list.head.next = second; // Link first node with the second node
second.next = third; // Link second node with the third node
list.printListUsingLoop();
}
}
```
#### Using Recursion
Below is the Java code to print the elements of a linked list using recursion:
```java
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class LinkedList {
Node head; // head of the list
/* Function to print linked list using recursion starting from head.next */
void printListUsingRecursion(Node node) {
if (node == null) {
return; // Base case: end of the list
}
System.out.print(node.data + " ");
printListUsingRecursion(node.next);
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Creating a simple linked list
list.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
list.head.next = second; //
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images

Recommended textbooks for you

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON

Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science

Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning

Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning

Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education

Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY