Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*******IN JAVA******** Given a MyLinkedList class, the task is to implement a method to find the n'th node from the end of the list. public

*******IN JAVA********

Given a MyLinkedList class, the task is to implement a method to find the n'th node from the end of the list. public static int getNthFromLast(MyLinkedList list, int n)

- The method getNthFromLast takes two arguments. the MyLinkedLIst list and an integer n. - youre allowed to use the provided methods in the given MyLinkeList class.

INPUT Every test case contains two lines - first line of a test case contains data items of linked list - second line of a test case contains value of n OUTPUT - Output a single integer that is the nth integer in the linked list from the end - print -1 if the value of n is zero or greater than the number of elements in the linked list

EX: Input 1: 1 2 3 4 5 6 7 8 9 2 Output 1: 8 ------------------- Input2: 1 2 3 4 5 Output2: -1 -------------------- Input3: 1 2 3 4 5 6 0 Output3: -1 ---------------------

*********Use Following source code*************

****************************************************

import java.util.*;

public class MyLinkedList {

private Node first;

private Node last;

private int size;

/*

* add element e to the end of the linkedlist

* @param e

*/

public void addLast(int e) {

Node newNode = new Node(e);

if(first == null && last ==null) {

first = newNode;

}else {

last.setNext(newNode);

}

last = newNode;

size++;

}

public Node getHead() {

return this.first;

}

public int getSize() {

return this.size;

}

}

class Node{

private int element;

private Node next;

public Node(int e) {

this.element = e;

}

public int getElement() {

return element;

}

public Node getNext() {

return next;

}

public void setElement (int element) {

this.element = element;

}

public void setNext(Node next) {

this.next = next;

}

}

***************************************************

public class Practice {

// Find the nth node from the end of the MyLinkedList list

public static int getNthFromLast(MyLinkedList list, int n) {

/// CODE HERE

}

}

**************************************************

import java.util.*;

public class DriverMain {

public static void main (String [] args) {

Scanner in = new Scanner(System.in);

String s = in.nextLine();

int n = in.nextInt();

in.close();

int[] arr = Arrays.stream(s.substring(0, s.length()).split("\\s"))

.map(String::trim).mapToInt(Integer::parseInt).toArray();

// construct MyLinkedList

MyLinkedList list = new MyLinkedList();

for(int i=0;i

list.addLast(arr[i]);

}

// print the nth node from the end.

System.out.print(Final.getNthFromLast(list, n));

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

Describe several ways to identify high-potential employees.

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago