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 node1 = Node(3) node2 = Node(5) node3 = Node(13) node4 = Node(2) node1.next = node2 node2.next = node3 node3.next = node4 currentNode = node1 while currentNode: print(currentNode.data, end=" -> ") currentNode = currentNode.next print("null") #Python
#include
#include
// Define the Node struct typedef struct Node { int data; struct Node* next; } Node; // Create a new node Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory allocation failed!\n"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } // Print the linked list void printList(Node* node) { while (node) { printf("%d -> ", node->data); node = node->next; } printf("null\n"); } int main() { // Explicitly creating and connecting nodes Node* node1 = createNode(3); Node* node2 = createNode(5); Node* node3 = createNode(13); Node* node4 = createNode(2); node1->next = node2; node2->next = node3; node3->next = node4; printList(node1); // Free the memory free(node1); free(node2); free(node3); free(node4); 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 main(String[] args) { // Creating individual nodes Node firstNode = new Node(3); Node secondNode = new Node(5); Node thirdNode = new Node(13); Node fourthNode = new Node(2); // Linking nodes together firstNode.next = secondNode; secondNode.next = thirdNode; thirdNode.next = fourthNode; // Printing linked list Node currentNode = firstNode; while (currentNode != null) { System.out.print(currentNode.data + " -> "); currentNode = currentNode.next; } System.out.println("null"); } } //Java
Python result:
C result:
Java result:
3 -> 5 -> 13 -> 2 -> null
3 -> 5 -> 13 -> 2 -> null
3 -> 5 -> 13 -> 2 -> null