Question

In: Computer Science

PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.

PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.

Solutions

Expert Solution

We need to check if the node is null or not. If the node is null then it will be zero, if it is not then we will add one to the length of next of node.

Python Code:

# Node class
class Node:
   # Function for initialisation of the node object
   def __init__(self, data):
       self.data = data # Assign data
       self.next = None # Initialize next as null


# Linked List class contains a Node object
class LinkedList:

   # Function to initialize head
   def __init__(self):
       self.head = None


   # This function is in LinkedList class. It inserts a new node at the starting of Linked List.
   def push(self, new_data):

       # 1 & 2: Allocate the Node &
       #   Put in the data
       new_node = Node(new_data)

       # 3. Make next of new Node as head
       new_node.next = self.head

       # 4. Move the head to point to new Node
       self.head = new_node


   # This function counts number of nodes in Linked List iteratively, given 'node' as the beginning node.
   def getCount(self):
       temp = self.head # Initialise temp
       count = 0 # Initialise count

       # Loop while end of linked list is not reached
       while (temp):
           count += 1
           temp = temp.next
       return count


# Code execution begins from here
if __name__=='__main__':
   llist = LinkedList()
   llist.push(1)
   llist.push(3)
   llist.push(1)
   llist.push(2)
   llist.push(1)
   print ("Number of nodes are :",llist.getCount())

Output:

Number of nodes are : 5

Screenshot (online compiler used to run the code):

Please comment in case of any doubt.
Please upvote if this helps.


Related Solutions

1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the...
Problem Description: Using Python write a Singly‐Linked List (with only the Head pointer) that supports the following operations: 1. Adding an element at the middle of the list. 2. Removing the middle element of the list (and return the data). 3. Adding an element at a given index. 4. Removing an element at a given index (and return the data). For #1 and #2, ignore the operations if the length of the list is an even number. For #3, if...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last()...
In python: implement a singly linked list with following functions: - add_head(e) - add_tail(e) - find_3rd_to_last() - returns element located at third-to-last in the list - reverse() - reveres the linked list, note, this is not just printing elements in reverse order, this is actually reversing the list
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT