Saturday, June 19, 2021
Home coding Write a program to print a linkedlist.

Write a program to print a linkedlist.

Given a linkedlist,we have to print a linkedlist of given size.

Example:

Input:1 2 3 4 5 6
Output:1 2 3 4 5 6 

Logic:

STEP 1:Traverse a whole linkedlist till head node get reached to null node.
STEP 2:print the value of head node.
STEP 3:traverse the next node of head.

Code

void printLinkedlist(Node *head)
{
    if(head==NULL)
    {
        return;
    }
    while(head!=NULL)
    {
        cout<<head->data<<" ";
        head=head->next;
    }
}

Ouput:

1 2 3 4 5 6

Time Complexity:O(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments