Data structures and algorithms in C++
Data structures and algorithms in C++
2nd Edition
ISBN: 9780470460443
Author: Goodrich
Publisher: WILEY
Expert Solution & Answer
Book Icon
Chapter 3, Problem 7R

Explanation of Solution

The algorithm to find the penultimate of the singly linked list:

Algorithm:

1. Find the length of linked list till the end.

2. Print the (len-1) nth node of linked list from the beginning.

Explanation:

  • The above algorithm is a simple algorithm to find the second last element of a singly linked list.
  • In the algorithm there are two steps, the first one says that find the length of the linked list.
  • The second step says that, print the elements up to nth node of the linked list.

Psuedocode for finding 2nd last element:

Function : find2ndLast(head, data)

1. Create variable len=0 and create temp pointer and assign the value of head;

2. Count the number of node in linked list till temp is not equal to null

while(temp!=NULL)

temp= temp->next;

len ++

end while

3. Check if value of n is not more than length of the linked list

  if len less than data then return;

4...

Blurred answer
Knowledge Booster
Background pattern image
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