Data Structures | Linked List | Question 17

Last Updated :
Discuss
Comments

Consider the following function to traverse a linked list. 

C++
// C++ version of traverse function
void traverse(Node *head) {
    while (head != NULL) {
        printf("%d  ", head->data);
        head = head->next;
    }
}
C
void traverse(struct Node *head)
{
   while (head->next != NULL)
   {
       printf("%d  ", head->data);
       head = head->next;
   }
}
Java
// Java version of traverse function
void traverse(Node head) {
    while (head != null) {
        System.out.print(head.data + "  ");
        head = head.next;
    }
}
Python
# Python 3 version of traverse function
def traverse(head):
    while head is not None:
        print(head.data, end='  ')
        head = head.next
JavaScript
// JavaScript version of traverse function
function traverse(head) {
    while (head !== null) {
        console.log(head.data + '  ');
        head = head.next;
    }
}

Which of the following is FALSE about above function?

The function may crash when the linked list is empty

The function doesn't print the last node when the linked list is not empty

The function is implemented incorrectly because it changes head

None of the above

Share your thoughts in the comments