SlideShare a Scribd company logo
Java
i have to make a method that takes a linked list and then returns a new linked list that is the
reverse of the original
this is the starting code
/**
* LinkedList lab.
* @author Emma Cheney
* @version 10/2/23
*/
public class LinkedList {
/**
* Internal Node class used for the linked list.
*/
public class Node {
String key;
int value;
Node next;
/**
* Node Constructor.
* @param key The Key
* @param value The Value
*/
public Node(String key, int value) {
this.key = key;
this.value = value;
}
}
private Node head;
private Node tail;
/**
* The default constructor.
*/
public LinkedList() {
head = null;
tail = null;
}
/**
* Add the key, value pair to the head of the linkedlist.
* @param key The Key
* @param val The Value
*
*/
public void addHead(String key, int val) {
Node n = new Node(key, val);
if (head == null) {
head = n;
tail = n;
} else {
n.next = head;
head = n;
}
}
/**
* Add the key, val pair to the tail of the linkedlist.
* @param key The Key
* @param val The Value
*/
public void addTail(String key, int val) {
Node n = new Node(key, val);
if (tail == null) {
head = n;
tail = n;
} else {
tail.next = n;
tail = n;
}
}
/**
* Returns the String format of the linkedlist.
* @return String The String format.
*/
public String toString() {
String ret = "";
Node curr = head;
while (curr != null) {
if (curr.next != null) {
ret += curr.key + ":" + curr.value + ", ";
} else {
ret += curr.key + ":" + curr.value;
}
curr = curr.next;
}
return ret;
}
/**
* Locate the Node in the linkedlist with the given key.
* @param key The key to find in the LinkedList
* @return Node Returns the Node with the given key or null if non-existent.
*/
private Node find(String key) {
Node curr = head;
while (curr != null) {
if (curr.key.equals(key)) {
return curr;
}
curr = curr.next;
}
return null;
} A static method called reversed that takes a LinkedList as a parameter and returns a new
LinkedList that is reversed. f; traverse through the input list (starting at the head). In order to
reverse the ordering, you need to add the current node's key and value onto the head of the new
list (addHead method). Do not change the input list or any values in it. Example (result in red)
//assumelistcontainsthesekeysandvalues://ie:6,bi:48,bi:73,bi:63LinkedList.reversed(list);bi:63,bi:
73,bi:48,ie:6

More Related Content

Similar to Javai have to make a method that takes a linked list and then retu.pdf (20)

PDF
Complete in JavaCardApp.javapublic class CardApp { private.pdf
MAYANKBANSAL1981
 
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
alphaagenciesindia
 
PDF
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
PDF
here is the starter code public class LinkedListPracticeLab.pdf
geetakannupillai1
 
PDF
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
fantasiatheoutofthef
 
PDF
please i need help Im writing a program to test the merge sort alg.pdf
ezonesolutions
 
PDF
Fix my codeCode.pdf
Conint29
 
PDF
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
rajeshjangid1865
 
PDF
In this assignment you will implement insert() method for a singly l.pdf
fantasiatheoutofthef
 
DOCX
Write a program that creates a LinkedList object of 10 characters- the.docx
lez31palka
 
PDF
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
PDF
Hi,I have added the methods and main class as per your requirement.pdf
annaelctronics
 
PDF
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
facevenky
 
PDF
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
PDF
In Java write a menu driven program that implements the following li.pdf
arihantelehyb
 
PDF
In this lab, we will write an application to store a deck of cards i.pdf
contact41
 
PDF
Implementation The starter code includes List.java. You should not c.pdf
maheshkumar12354
 
PDF
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
xlynettalampleyxc
 
PDF
Write a JAVA LinkedListRec class that has the following methods siz.pdf
info785431
 
PDF
Linked List Leetcode - Easy Collections - Interview Questions Java
Sunil Yadav
 
Complete in JavaCardApp.javapublic class CardApp { private.pdf
MAYANKBANSAL1981
 
Rewrite this code so it can use a generic type instead of integers. .pdf
alphaagenciesindia
 
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
here is the starter code public class LinkedListPracticeLab.pdf
geetakannupillai1
 
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
fantasiatheoutofthef
 
please i need help Im writing a program to test the merge sort alg.pdf
ezonesolutions
 
Fix my codeCode.pdf
Conint29
 
Objective Manipulate the Linked List Pointer.Make acopy of LList..pdf
rajeshjangid1865
 
In this assignment you will implement insert() method for a singly l.pdf
fantasiatheoutofthef
 
Write a program that creates a LinkedList object of 10 characters- the.docx
lez31palka
 
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
Hi,I have added the methods and main class as per your requirement.pdf
annaelctronics
 
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
facevenky
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
In Java write a menu driven program that implements the following li.pdf
arihantelehyb
 
In this lab, we will write an application to store a deck of cards i.pdf
contact41
 
Implementation The starter code includes List.java. You should not c.pdf
maheshkumar12354
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
xlynettalampleyxc
 
Write a JAVA LinkedListRec class that has the following methods siz.pdf
info785431
 
Linked List Leetcode - Easy Collections - Interview Questions Java
Sunil Yadav
 

More from stopgolook (15)

PDF
Jordano Food Products Supply Chain Profile Jordano Foods Tracie Shan.pdf
stopgolook
 
PDF
Java Code The traditional way to deal with these in Parsers is the .pdf
stopgolook
 
PDF
J.M. Baker worked as a traditional land use researcher and consultan.pdf
stopgolook
 
PDF
IT Project Management homework Identify any project of your choice.pdf
stopgolook
 
PDF
INSTRUCTIONSDevelop, and present a plan and business case for an a.pdf
stopgolook
 
PDF
In the realm of professional dynamics, understanding and appreciating .pdf
stopgolook
 
PDF
import React, { useEffect } from react;import { BrowserRouter as.pdf
stopgolook
 
PDF
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
stopgolook
 
PDF
Im trying to define a class in java but I seem to be having a bit o.pdf
stopgolook
 
PDF
in c languageTo determine the maximum string length, we need to .pdf
stopgolook
 
PDF
In 2011, the head of the Presidential Protection Force (for purposes.pdf
stopgolook
 
PDF
import java.util.Scanner;public class Main {private static i.pdf
stopgolook
 
PDF
If a taxpayer has investment income that exceeds a certain threshold.pdf
stopgolook
 
PDF
Im having an issue with the simulateOPT() methodthis is the p.pdf
stopgolook
 
PDF
I. Naive Robot Navigation ProblemDesign a program that uses the B.pdf
stopgolook
 
Jordano Food Products Supply Chain Profile Jordano Foods Tracie Shan.pdf
stopgolook
 
Java Code The traditional way to deal with these in Parsers is the .pdf
stopgolook
 
J.M. Baker worked as a traditional land use researcher and consultan.pdf
stopgolook
 
IT Project Management homework Identify any project of your choice.pdf
stopgolook
 
INSTRUCTIONSDevelop, and present a plan and business case for an a.pdf
stopgolook
 
In the realm of professional dynamics, understanding and appreciating .pdf
stopgolook
 
import React, { useEffect } from react;import { BrowserRouter as.pdf
stopgolook
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
stopgolook
 
Im trying to define a class in java but I seem to be having a bit o.pdf
stopgolook
 
in c languageTo determine the maximum string length, we need to .pdf
stopgolook
 
In 2011, the head of the Presidential Protection Force (for purposes.pdf
stopgolook
 
import java.util.Scanner;public class Main {private static i.pdf
stopgolook
 
If a taxpayer has investment income that exceeds a certain threshold.pdf
stopgolook
 
Im having an issue with the simulateOPT() methodthis is the p.pdf
stopgolook
 
I. Naive Robot Navigation ProblemDesign a program that uses the B.pdf
stopgolook
 
Ad

Recently uploaded (20)

PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Ad

Javai have to make a method that takes a linked list and then retu.pdf

  • 1. Java i have to make a method that takes a linked list and then returns a new linked list that is the reverse of the original this is the starting code /** * LinkedList lab. * @author Emma Cheney * @version 10/2/23 */ public class LinkedList { /** * Internal Node class used for the linked list. */ public class Node { String key; int value; Node next; /** * Node Constructor. * @param key The Key * @param value The Value */ public Node(String key, int value) { this.key = key; this.value = value; } }
  • 2. private Node head; private Node tail; /** * The default constructor. */ public LinkedList() { head = null; tail = null; } /** * Add the key, value pair to the head of the linkedlist. * @param key The Key * @param val The Value * */ public void addHead(String key, int val) { Node n = new Node(key, val); if (head == null) { head = n; tail = n; } else { n.next = head; head = n; } } /** * Add the key, val pair to the tail of the linkedlist. * @param key The Key
  • 3. * @param val The Value */ public void addTail(String key, int val) { Node n = new Node(key, val); if (tail == null) { head = n; tail = n; } else { tail.next = n; tail = n; } } /** * Returns the String format of the linkedlist. * @return String The String format. */ public String toString() { String ret = ""; Node curr = head; while (curr != null) { if (curr.next != null) { ret += curr.key + ":" + curr.value + ", "; } else { ret += curr.key + ":" + curr.value; } curr = curr.next;
  • 4. } return ret; } /** * Locate the Node in the linkedlist with the given key. * @param key The key to find in the LinkedList * @return Node Returns the Node with the given key or null if non-existent. */ private Node find(String key) { Node curr = head; while (curr != null) { if (curr.key.equals(key)) { return curr; } curr = curr.next; } return null; } A static method called reversed that takes a LinkedList as a parameter and returns a new LinkedList that is reversed. f; traverse through the input list (starting at the head). In order to reverse the ordering, you need to add the current node's key and value onto the head of the new list (addHead method). Do not change the input list or any values in it. Example (result in red) //assumelistcontainsthesekeysandvalues://ie:6,bi:48,bi:73,bi:63LinkedList.reversed(list);bi:63,bi: 73,bi:48,ie:6