From f481d6a2cac8e8a03886e3ceb7ffe818abebe6ae Mon Sep 17 00:00:00 2001 From: Madhav Mishra <59422042+carbseater@users.noreply.github.com> Date: Fri, 16 Oct 2020 19:34:33 +0530 Subject: [PATCH 1/2] Find length of linked list --- algorithms/linkedlist/find_length_linkedList | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 algorithms/linkedlist/find_length_linkedList diff --git a/algorithms/linkedlist/find_length_linkedList b/algorithms/linkedlist/find_length_linkedList new file mode 100644 index 0000000..7fe4cf7 --- /dev/null +++ b/algorithms/linkedlist/find_length_linkedList @@ -0,0 +1,56 @@ +# A complete working Python program to find length of a +# Linked List iteratively + +# Node class +class Node: + # Function to initialise 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 beginning 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 starting 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 starts here +if __name__=='__main__': + llist = LinkedList() + llist.push(1) + llist.push(3) + llist.push(1) + llist.push(2) + llist.push(1) + print ("Count of nodes is :",llist.getCount()) From 8e834e971836a74744507091bce48df04fbbd90e Mon Sep 17 00:00:00 2001 From: Madhav Mishra <59422042+carbseater@users.noreply.github.com> Date: Tue, 20 Oct 2020 23:00:00 +0530 Subject: [PATCH 2/2] Rename find_length_linkedList to find_length_linkedList.py --- .../{find_length_linkedList => find_length_linkedList.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename algorithms/linkedlist/{find_length_linkedList => find_length_linkedList.py} (100%) diff --git a/algorithms/linkedlist/find_length_linkedList b/algorithms/linkedlist/find_length_linkedList.py similarity index 100% rename from algorithms/linkedlist/find_length_linkedList rename to algorithms/linkedlist/find_length_linkedList.py pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy