Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA PLEASE AS SSON AS POSSIBLE NUMBER 1 Implement an algorithm to find the nth to the last element of a single linked list.

IN JAVA PLEASE AS SSON AS POSSIBLE

NUMBER 1

Implement an algorithm to find the nth to the last element of a single linked list.

If the list is:

8->10->5->3->2->1->5->4->10->10

then the result is 7th to last node is 3.

public static LinkedListNode nthToLast(LinkedListNode head, int nth)

{

//todo

}

NUMBER 2

Complete the method below, which should return a new stack containing the playing cards in the given stack in the same order, with all aces removed. If the s.getRank() method equals 1, then the card is an Ace.

public static Stack removeAces(Stack s)

{

//todo

}

NUMBER 3

A singly linked list of integers has a dummy head node, followed by a node with 8, followed by a node with 9, followed by a node with 1, and a final node with 3.

h->8->9->1->3

a) What does the following code fragment output for the list above? What does it output in general? What must be true about this list to avoid a runtime error?

Node p = head;

while (p.next != null) {

p = p.next;

}

System.out.println(p.data);

b) The following code outputs the sum of all of the values in the linked list. Explain the unintended side effect that occurs when this code is run. Then rewrite the code to avoid this side effect.

int s = 0;

while (head != null) {

s += head.data;

head = head.next;

}

System.out.println(s);

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

Step: 3

blur-text-image

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

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions

Question

Pins Framings th number it the variance is unfavorable. Do the

Answered: 1 week ago

Question

What is the orientation toward time?

Answered: 1 week ago

Question

4. How is culture a contested site?

Answered: 1 week ago