Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781119278023
Author: Michael T. Goodrich; Roberto Tamassia; Michael H. Goldwasser
Publisher: Wiley Global Education US
Expert Solution & Answer
Book Icon
Chapter 3, Problem 6R

Explanation of Solution

Method to find the second-to-last node in singly linked list to indicates the last node as null next reference:

//Define the secondToLast() method

private Node<E> secondToLast( )

{

  //Check whether the size is less than "2"

  if (size < 2)

  //Throw an exception

throw new IllegalStateException("List contains two or more entries");

  //Create a node to hold the head value

Node<E> ptr = head;

/*Loop executes until the "ptr" is not equal to "null". */

  while (ptr−>next−>next != null)

/*Second to last node is determined and assigned to "ptr"...

Blurred answer
Students have asked these similar questions
show work
show work on paper
show work on paper
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Systems Architecture
Computer Science
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr