Displaying the Nodes
Iterative and recursive method to print all the nodes in a linked list.
void display(struct node *ptr) {
// Checking if list is empty
if (ptr == NULL) {
printf("The list is empty\n");
}
// Iterate till the end otherwise
else {
while (ptr != NULL) {
printf("%d\t", ptr -> data);
ptr = ptr -> next;
}
}
}
void display(struct node *ptr) {
if (ptr != NULL) {
printf("%d\t", ptr -> data);
display(ptr -> next);
}
}
void display(struct node *ptr) {
if (ptr != NULL) {
display(ptr -> next);
printf("%d\t", ptr -> data);
}
}
Iterative
Recursive
Time Complexity : O(n)
No extra space
Time complexity : O(n)
Internal stack of size n+1 is used.
Contributed by Nitin Ranganath