What the code is about: Implement a recursive algorithm to add all the elements of a non-dummy headed singly linked linear list. Only head of the list will be given as parameter where you may assume every node can contain only integer as its element. Note: you’ll need a Singly Node class for this code. **PLEASE EXPLAIN HOW THE NODE CLASS AND THE CONSTRUCTOR OF THE NODE CLASS IS WORKING IN THIS CODE** #singlty node class for single linked list class node: def __init__(self, value = None, next=None): self.value = value
What the code is about:
Implement a recursive
Note: you’ll need a Singly Node class for this code.
**PLEASE EXPLAIN HOW THE NODE CLASS AND THE CONSTRUCTOR OF THE NODE CLASS IS WORKING IN THIS CODE**
#singlty node class for single linked list
class node:
def __init__(self, value = None, next=None):
self.value = value
self.next = next
def AddAll(head):#takes head of single linked list head
if head==None:
return 0#if reached end of the linked list
return AddAll(head.next) + head.value
#each node's next pointer is passed in recursive call
#and value of each node is added while returning from recursive call
Step by step
Solved in 3 steps with 2 images