Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Implement a function that counts the number of nodes in a circularly linked list

# Python3 program to count number of nodes in
# a circular linked list.
 
# structure for a node
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Function to insert a node at the beginning
# of a Circular linked list */
def push(head_ref,data):
 
    ptr1 = Node(0)
    temp = head_ref
    ptr1.data = data
    ptr1.next = head_ref
 
    # If the linked list is not None then set
    # the next of last node
    if (head_ref != None) :
        while (temp.next != head_ref):
            temp = temp.next
        temp.next = ptr1
    else:
        ptr1.next = ptr1 #For the first node */
 
    head_ref = ptr1
    return head_ref
 
# Function to print nodes
# in a given Circular linked list
def countNodes(head):
 
    temp = head
    result = 0
    if (head != None) :
        while True :
            temp = temp.next
            result = result + 1
            if (temp == head):
                break
     
    return result
 
# Driver Code
if __name__=='__main__':
 
    # Initialize lists as empty */
    head = None
    head = push(head, 12)
    head = push(head, 56)
    head = push(head, 2)
    head = push(head, 11)
 
    print( countNodes(head))
     
# This code is contributed by Arnab Kundu
Comment

PREVIOUS NEXT
Code Example
Typescript :: nativescript alert 
Typescript :: Stack list of widgets timed flutter 
Typescript :: how to invert sortField primeng 
Typescript :: Integer Which of the following can be described as the decision whether to obtain the necessary software from internal or external sources?and Development Environment meaning 
Typescript :: centos remote desktop clients vs remote management for linux 
Typescript :: dependencymanagement imports mavenbom 
Typescript :: java sort list by attribute 
Typescript :: firebase not found in envirorment.ts file angular 
Typescript :: to move a directory with its contents in terminal in linux 
Typescript :: laravel Adding shipping rate to checkout session results in "invalid array" exception 
Typescript :: re initialize weights keras 
Typescript :: how to checka query to return User whose first name starts with R or last name starts with D in django 
Typescript :: nextjs and nodemailer problem after deploy 
Typescript :: get all fields of mongoose schema typescript 
Typescript :: como agregarle un rango a mat-datapicker 
Typescript :: 3 dots react 
Typescript :: running same tests against different data 
Typescript :: install dependencies angular 
Typescript :: angular build Failed to load resource 
Typescript :: tiqets endpoints 
Cpp :: how to convert string to wchar_t in c++ 
Cpp :: qt debug 
Cpp :: How to make two dimensional string in c++ 
Cpp :: c++ round number down 
Cpp :: c++ example 
Cpp :: for loop vector 
Cpp :: check if double is integer c++ 
Cpp :: exp() c++ 
Cpp :: how to load from files C++ 
Cpp :: c++ remove last element from vector 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =