Problem Statement:
Design and implement a simple singly linked list using basic building blocks like nodes and references (or pointers). The goal is to:
- Define a node with a value and a pointer to the next node.
- Create individual nodes and link them together to form a list.
- Traverse the list from the head and print each node’s value.
This helps you understand how data is stored and accessed sequentially in memory using pointers or references.
Example:
Input: Create 3 nodes with values 10, 20, 30 and link them in order.
Output: Print values in sequence: 10 20 30
Approach:
- 1. Define a
Node
class or structure with aval
andnext
pointer. - 2. Create individual nodes (e.g., node1, node2, node3).
- 3. Link the nodes: node1.next → node2, node2.next → node3.
- 4. Set the
head
of the list to the first node. - 5. Traverse the list using a loop until the current node becomes
null
(orNone
). - 6. Print the value at each node.
Explanation:
- Each node stores data and a reference to the next node.
- The list is connected by linking nodes via the
next
field. - The traversal starts from the head and moves node by node until the end.
- This basic structure forms the foundation for many advanced data structures like stacks, queues, and graph adjacency lists.
function Node(val) {
this.val = val;
this.next = null;
}
function MyLinkedList() {
this.head = null;
this.size = 0;
}
const list = new MyLinkedList();
const node1 = new Node(10);
const node2 = new Node(20);
const node3 = new Node(30);
node1.next = node2;
node2.next = node3;
list.head = node1;
list.size = 3;
let current = list.head;
while (current) {
console.log(current.val);
current = current.next;
}
class Node:
def __init__(self, val):
self.val = val
self.next = None
class MyLinkedList(object):
def __init__(self):
pass
# Create and print manually for testing
node1 = Node(10)
node2 = Node(20)
node3 = Node(30)
node1.next = node2
node2.next = node3
current = node1
while current:
print(current.val)
current = current.next
class Node {
int val;
Node next;
Node(int val) {
this.val = val;
this.next = null;
}
}
class MyLinkedList {
public MyLinkedList() {
}
}
public class Main {
public static void main(String[] args) {
Node node1 = new Node(10);
Node node2 = new Node(20);
Node node3 = new Node(30);
node1.next = node2;
node2.next = node3;
Node current = node1;
while (current != null) {
System.out.println(current.val);
current = current.next;
}
}
}
#include <iostream>
using namespace std;
struct Node {
int val;
Node* next;
Node(int v) : val(v), next(nullptr) {}
};
class MyLinkedList {
public:
MyLinkedList() {}
};
int main() {
Node* node1 = new Node(10);
Node* node2 = new Node(20);
Node* node3 = new Node(30);
node1->next = node2;
node2->next = node3;
Node* current = node1;
while (current != nullptr) {
cout << current->val << endl;
current = current->next;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int val;
struct Node* next;
} Node;
typedef struct {
} MyLinkedList;
MyLinkedList* myLinkedListCreate() {
return NULL;
}
// Extra: Create and print nodes
int main() {
Node* node1 = (Node*)malloc(sizeof(Node));
Node* node2 = (Node*)malloc(sizeof(Node));
Node* node3 = (Node*)malloc(sizeof(Node));
node1->val = 10;
node2->val = 20;
node3->val = 30;
node1->next = node2;
node2->next = node3;
node3->next = NULL;
Node* current = node1;
while (current != NULL) {
printf("%d\n", current->val);
current = current->next;
}
return 0;
}
using System;
public class Node {
public int val;
public Node next;
public Node(int val) {
this.val = val;
this.next = null;
}
}
public class MyLinkedList {
public MyLinkedList() {}
}
public class Program {
public static void Main() {
Node node1 = new Node(10);
Node node2 = new Node(20);
Node node3 = new Node(30);
node1.next = node2;
node2.next = node3;
Node current = node1;
while (current != null) {
Console.WriteLine(current.val);
current = current.next;
}
}
}