Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class Node: def __init__(self, data): self.data = data self.next = None def traverseAndPrint(head): currentNode = head while currentNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("null") node1 = Node(7) node2 = Node(11) node3 = Node(3) node4 = Node(2) node5 = Node(9) node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node5 traverseAndPrint(node1) #Python
#include
#include
typedef struct Node { int data; struct Node* next; } Node; void traverseAndPrint(Node* head) { Node* currentNode = head; while (currentNode) { printf("%d -> ", currentNode->data); currentNode = currentNode->next; } printf("null\n"); } int main() { Node* node1 = (Node*)malloc(sizeof(Node)); Node* node2 = (Node*)malloc(sizeof(Node)); Node* node3 = (Node*)malloc(sizeof(Node)); Node* node4 = (Node*)malloc(sizeof(Node)); Node* node5 = (Node*)malloc(sizeof(Node)); node1->data = 7; node2->data = 11; node3->data = 3; node4->data = 2; node5->data = 9; node1->next = node2; node2->next = node3; node3->next = node4; node4->next = node5; node5->next = NULL; traverseAndPrint(node1); // Free the allocated memory free(node1); free(node2); free(node3); free(node4); free(node5); return 0; } //C
public class Main { static class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public static void traverseAndPrint(Node head) { Node currentNode = head; while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("null"); } public static void main(String[] args) { Node node1 = new Node(7); Node node2 = new Node(11); Node node3 = new Node(3); Node node4 = new Node(2); Node node5 = new Node(9); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; traverseAndPrint(node1); } } //Java
Python result:
C result:
Java result:
7 -> 11 -> 3 -> 2 -> 9 -> null
7 -> 11 -> 3 -> 2 -> 9 -> null
7 -> 11 -> 3 -> 2 -> 9 -> null