Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
6th Edition
ISBN: 9781118771334
Author: Michael T. Goodrich
Publisher: WILEY
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
The optimal number of elements for a linked list is unknown.Which of the following is the longest?
Given a singly linked list, print reverse of it using a recursive function printLinkedList( node *first ) where first is the pointer pointing to the first data node. For example, if the given linked list is 1->2->3->4, then output should be: 4 3 2 1 (note the whitespace in between each data value)
Implement a function to insert a node at the end of a doubly linked list. Explain the steps involved and analyze the time complexity of your implementation.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education