Length of LL
For a given singly linked list of integers, find and return its length. Do it using an iterative method.
Input format :
Remember/Consider :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self,data): | |
self.data=data | |
self.next=None | |
def LinkedLists(arr): | |
head=None | |
tail=None | |
for i in arr: | |
if i== -1: | |
break | |
else: | |
NewNode=Node(i) | |
if head==None: | |
head=NewNode | |
tail=NewNode | |
else: | |
tail.next=NewNode | |
tail=NewNode | |
return head | |
def printLL(head): | |
count=0 | |
while head is not None: | |
head=head.next | |
count=count+1 | |
return count | |
for i in range(int(input())): | |
arr = list(map(int, input().split())) | |
print(printLL(LinkedLists(arr))) | |
Comments
Post a Comment
Please give us your valuable feedback