We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here, we have a function printLinkedList that takes in a reference to the head of the linked list. We initialize a variable current to the head of the list. Then we loop through the list using a while loop until current becomes null (which means we've reached the end of the list). Within the loop, we print the data of the current node using console.log(current.data) and update the current variable to point to the next node in the list using current = current.next.
Note that we assume that the linked list is implemented using nodes with data and next properties, where data stores the value of the node and next points to the next node in the list.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Print the Elements of a Linked List
You are viewing a single comment's thread. Return to all comments →
Here, we have a function printLinkedList that takes in a reference to the head of the linked list. We initialize a variable current to the head of the list. Then we loop through the list using a while loop until current becomes null (which means we've reached the end of the list). Within the loop, we print the data of the current node using console.log(current.data) and update the current variable to point to the next node in the list using current = current.next.
Note that we assume that the linked list is implemented using nodes with data and next properties, where data stores the value of the node and next points to the next node in the list.