Skip to content

Commit

Permalink
Create Length of Linked List (iterative)
Browse files Browse the repository at this point in the history
  • Loading branch information
C0DE-RUNNER authored Oct 5, 2020
1 parent 92dcdf2 commit 498c47b
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions C++/Length of Linked List (iterative)
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>

class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};

using namespace std;


Node *takeinput()
{
int data;
cin >> data;
Node *head = NULL, *tail = NULL;
while (data != -1)
{
Node *newNode = new Node(data);
if (head == NULL)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
cin >> data;
}
return head;
}

int length(Node *head)
{
int len=0;
while(head!=NULL){
len++;
head=head->next;
}
return len;
}

int main()
{
int t;
cin >> t;
while (t--)
{
Node *head = takeinput();
cout << length(head) << endl;
}
return 0;
}

0 comments on commit 498c47b

Please sign in to comment.