The length of list is total num of nodes it contains. So am empty list has length 0. For example consider the below linked list.
Below linked list : has 4 nodes, I,J,K and L. Here node L is last node and its tail is terminator. The length of this list is 4.
I -> J -> K -> L ->
I am trying to write a function int solve(IntList * L) {
} given a non empty linked list L consisting of N nodes , returns its length.Here this function must return 4, assuming N ranges [1..5,000] and list L doesnt have a cycle (each non empty pointer points to different structure)/
> friend ostream & operator<<(ostream &, constIntListNode &);
The fact that this won't even compile, coupled with the lack of any indentation at all, suggests you just copy/pasted someone else's HTML page in the attempt to pass off something as your own work.
Thanks for your detailed reply.
I am trying to write a function int solve(IntListNode * L) {
}
given a non empty linked list L consisting of N nodes , returns its length.
Here this function must return 4, assuming N ranges [1..5,000] and list L does not have a cycle (each non empty pointer points to different structure)
#include <iostream>
#include <iterator>
struct toy_list_node
{
int value ;
toy_list_node* next = nullptr ;
};
std::size_t size( const toy_list_node* n ) // return the length of the list
{
if( n == nullptr ) return 0 ; // empty list, size is zero
elsereturn 1 + size( n->next ) ; // otherwise size is one (for head) plus the size of what is there after head
}void print( const toy_list_node* n )
{
if( n == nullptr ) std::cout << "null\n" ;
else
{
std::cout << n->value << " => " ;
print( n->next ) ;
}
}
int main()
{
// set up a linked list of nodes
toy_list_node nodes[] { {1}, {2}, {3}, {4}, {5}, {6}, {7} } ;
for( std::size_t i = 0 ; i < std::size(nodes)-1 ; ++i ) nodes[i].next = nodes+i+1 ;
const toy_list_node* head = nodes ;
print(head) ; // print the linked list
std::cout << "size: " << size(head) << '\n' ; // compute and print its size
}
Your code looks more like a stack implementation based upon a list than a general list. Are you asking the question - given a stack which doesn't maintain a size element and which can't be iterated outside of the class definition - how do you determine the number of elements on the stack?
In this implementation logic, the linked list : has 4 nodes, I,J,K and L.
I -> J -> K -> L ->
Here node L is last node and its tail is terminator. The length of this list is 4.
The logic is more of stack based implementation, I am trying to write a function int solve(IntListNode * L) which must return 4.
Based on the structure declaration below, I am trying to write this function int solve(IntListNode * L). Here the pointer is called linked list if:
1) it is an empty pointer ( it is then called terminator or an empty list) or
2) it points to structure (called node or the head), that contains a value and linked list (called tail).
1 2 3 4 5 6 7 8 9
//struct declaration
struct IntListNode {
int Value;
IntListNode *next;
};