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;...
You are given a singly linked list. Write a function to find if the linked list...
You are given a singly linked list. Write a function to find if the linked list contains a cycle or not. A linked list may contain a cycle anywhere. A cycle means that some nodes are connected in the linked list. It doesn't necessarily mean that all nodes in the linked list have to be connected in a cycle starting and ending at the head. You may want to examine Floyd's Cycle Detection algorithm. /*This function returns true if given...
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 Python, I've created a Node class for implementing a singly linked list. My Code: class...
In Python, I've created a Node class for implementing a singly linked list. My Code: class Node: def __init__(self,initdata): self.data = initdata self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self,newdata): self.data = newdata def setNext(self,newnext): self.next = newnext class SinglyLinkedList: def __init__(self): self.head = None def add(self,key): addkey = Node(key) addkey.setNext(self.head) self.head = addkey Now the question is: Create an append method that is O(1) by modifying the constructor of the SinglyLinkedList class by adding...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT