{"id":6648,"date":"2025-06-12T16:35:45","date_gmt":"2025-06-12T11:05:45","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6648"},"modified":"2025-06-12T17:42:35","modified_gmt":"2025-06-12T12:12:35","slug":"implement-a-singly-linked-list-code-in-js-c-c-java-python-c","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/implement-a-singly-linked-list-code-in-js-c-c-java-python-c\/","title":{"rendered":"Implement a singly linked list in JS, C, C++, Java, Python, C#"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<!-- PrismJS for Syntax Highlighting -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\" rel=\"stylesheet\">\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_main-heading {\n  text-align: center;\n  font-size: 2.4rem;\n  color: #E58C32;\n  margin-top: 2.5rem;\n  font-weight: bold;\n}\n\n.wp_blog_explanation,\n.wp_blog_code-tabs-container {\n  max-width: 940px;\n  margin: 2rem auto;\n  padding: 2rem;\n  border-radius: 12px;\n  background-color: #f9fafb;\n}\n\n.wp_blog_explanation h2 {\n  font-size: 1.4rem;\n  color: #E58C32;\n  margin-bottom: 0.75rem;\n}\n\n.wp_blog_explanation p,\n.wp_blog_explanation li {\n  font-size: 1.05rem;\n  line-height: 1.7;\n  margin: 0.5rem 0;\n  color: #1f2937;\n}\n\n.wp_blog_explanation code {\n  padding: 3px 6px;\n  border-radius: 4px;\n  background: #e5e7eb;\n  font-family: 'Courier New', monospace;\n}\n\n.wp_blog_explanation img {\n  width: 100%;\n  border-radius: 12px;\n  margin-top: 1rem;\n}\n\n\/* Tabs *\/\n.wp_blog_code-tabs-header {\n  display: flex;\n  flex-wrap: wrap;\n  gap: 0.5rem;\n  margin-bottom: 1rem;\n}\n\n.wp_blog_code-tab-button {\n  padding: 0.6rem 1.2rem;\n  border: 1px solid #E58C32;\n  color: #E58C32;\n  border-radius: 50px;\n  font-weight: 600;\n  cursor: pointer;\n  background-color: white;\n  transition: background 0.3s ease, color 0.3s ease;\n}\n\n.wp_blog_code-tab-button.active {\n  background: #E58C32;\n  color: white;\n}\n\n\/* Code Panel *\/\n.wp_blog_code-tab-content {\n  display: none;\n  background: #111827;\n  border-radius: 12px;\n}\n\n.wp_blog_code-tab-content.active {\n  display: block;\n}\n\n.wp_blog_code-tab-content pre {\n  margin: 0;\n  padding: 1.5rem;\n  font-size: 1rem;\n  overflow-x: auto;\n  color: #f3f4f6;\n  background: #111827;\n  border-radius: 12px;\n}\n\n<\/style>\n<div class=\"wp_blog_explanation\">\n  <h2>Problem Statement:<\/h2>\n  <p>Design and implement a simple singly linked list using basic building blocks like nodes and references (or pointers). The goal is to:\n    <ul>\n      <li><strong>Define a node<\/strong> with a value and a pointer to the next node.<\/li>\n      <li><strong>Create individual nodes<\/strong> and link them together to form a list.<\/li>\n      <li><strong>Traverse the list<\/strong> from the head and print each node\u2019s value.<\/li>\n    <\/ul>\n    <p>This helps you understand how data is stored and accessed sequentially in memory using pointers or references.<\/p>\n  <\/p>\n  \n  <h2>Example:<\/h2>\n  <ul>\n    <li><p><strong>Input:<\/strong> Create 3 nodes with values 10, 20, 30 and link them in order.<\/p><\/li>\n    <li><p><strong>Output:<\/strong> Print values in sequence: 10 20 30<\/p><\/li>\n  <\/ul>\n  \n  <h2>Approach:<\/h2>\n  <ul>\n    <li>1. Define a <code>Node<\/code> class or structure with a <code>val<\/code> and <code>next<\/code> pointer.<\/li>\n    <li>2. Create individual nodes (e.g., node1, node2, node3).<\/li>\n    <li>3. Link the nodes: node1.next \u2192 node2, node2.next \u2192 node3.<\/li>\n    <li>4. Set the <code>head<\/code> of the list to the first node.<\/li>\n    <li>5. Traverse the list using a loop until the current node becomes <code>null<\/code> (or <code>None<\/code>).<\/li>\n    <li>6. Print the value at each node.<\/li>\n  <\/ul>\n  \n  <h2>Explanation:<\/h2>\n  <ul>\n    <li>Each node stores data and a reference to the next node.<\/li>\n    <li>The list is connected by linking nodes via the <code>next<\/code> field.<\/li>\n    <li>The traversal starts from the head and moves node by node until the end.<\/li>\n    <li>This basic structure forms the foundation for many advanced data structures like stacks, queues, and graph adjacency lists.<\/li>\n  <\/ul>\n  <\/div>\n  \n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cs\">C#<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nfunction Node(val) {\n  this.val = val;\n  this.next = null;\n}\n\nfunction MyLinkedList() {\n  this.head = null;\n  this.size = 0;\n}\n\nconst list = new MyLinkedList();\n\nconst node1 = new Node(10);\nconst node2 = new Node(20);\nconst node3 = new Node(30);\n\nnode1.next = node2;\nnode2.next = node3;\n\nlist.head = node1;\nlist.size = 3;\n\nlet current = list.head;\nwhile (current) {\n  console.log(current.val);\n  current = current.next;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\nclass Node:\n    def __init__(self, val):\n        self.val = val\n        self.next = None\n\nclass MyLinkedList(object):\n    def __init__(self):\n        pass\n\n\n# Create and print manually for testing\nnode1 = Node(10)\nnode2 = Node(20)\nnode3 = Node(30)\n\nnode1.next = node2\nnode2.next = node3\n\ncurrent = node1\nwhile current:\n    print(current.val)\n    current = current.next\n    <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n  <pre><code class=\"language-java\">\nclass Node {\n  int val;\n  Node next;\n\n  Node(int val) {\n      this.val = val;\n      this.next = null;\n  }\n}\n\nclass MyLinkedList {\n\n  public MyLinkedList() {\n  }\n}\n\npublic class Main {\n  public static void main(String[] args) {\n      Node node1 = new Node(10);\n      Node node2 = new Node(20);\n      Node node3 = new Node(30);\n\n      node1.next = node2;\n      node2.next = node3;\n\n      Node current = node1;\n      while (current != null) {\n          System.out.println(current.val);\n          current = current.next;\n      }\n  }\n}\n  <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n  <pre><code class=\"language-cpp\">\n#include &lt;iostream&gt;\nusing namespace std;\n\nstruct Node {\n  int val;\n  Node* next;\n  Node(int v) : val(v), next(nullptr) {}\n};\n\nclass MyLinkedList {\npublic:\n  MyLinkedList() {}\n};\n\nint main() {\n  Node* node1 = new Node(10);\n  Node* node2 = new Node(20);\n  Node* node3 = new Node(30);\n\n  node1->next = node2;\n  node2->next = node3;\n\n  Node* current = node1;\n  while (current != nullptr) {\n      cout &lt;&lt; current->val &lt;&lt; endl;\n      current = current->next;\n  }\n\n  return 0;\n}\n  <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n  <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\ntypedef struct Node {\n  int val;\n  struct Node* next;\n} Node;\n\ntypedef struct {\n} MyLinkedList;\n\nMyLinkedList* myLinkedListCreate() {\n  return NULL;\n}\n\n\n\/\/ Extra: Create and print nodes\nint main() {\n  Node* node1 = (Node*)malloc(sizeof(Node));\n  Node* node2 = (Node*)malloc(sizeof(Node));\n  Node* node3 = (Node*)malloc(sizeof(Node));\n\n  node1->val = 10;\n  node2->val = 20;\n  node3->val = 30;\n\n  node1->next = node2;\n  node2->next = node3;\n  node3->next = NULL;\n\n  Node* current = node1;\n  while (current != NULL) {\n      printf(\"%d\\n\", current->val);\n      current = current->next;\n  }\n\n  return 0;\n}\n  <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp_blog_code-tab-content\" data-lang=\"cs\">\n  <pre><code class=\"language-csharp\">\nusing System;\n\npublic class Node {\n  public int val;\n  public Node next;\n\n  public Node(int val) {\n      this.val = val;\n      this.next = null;\n  }\n}\n\npublic class MyLinkedList {\n\n  public MyLinkedList() {}\n}\n\npublic class Program {\n  public static void Main() {\n      Node node1 = new Node(10);\n      Node node2 = new Node(20);\n      Node node3 = new Node(30);\n\n      node1.next = node2;\n      node2.next = node3;\n\n      Node current = node1;\n      while (current != null) {\n          Console.WriteLine(current.val);\n          current = current.next;\n      }\n  }\n}\n  <\/code><\/pre>\n<\/div>\n\n<\/div>\n\n  \n  \n\n<script>\ndocument.addEventListener('DOMContentLoaded', () => {\nconst buttons = document.querySelectorAll('.wp_blog_code-tab-button');\nconst contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\nbuttons.forEach(button => {\nbutton.addEventListener('click', () => {\nconst lang = button.getAttribute('data-lang');\nbuttons.forEach(btn => btn.classList.remove('active'));\ncontents.forEach(content => content.classList.remove('active'));\nbutton.classList.add('active');\ndocument.querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`).classList.add('active');\n});\n});\n});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,176,811,810,172,173],"tags":[],"class_list":["post-6648","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-csharp","category-data-structures-and-algorithms","category-dsa","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6648"}],"version-history":[{"count":7,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6648\/revisions"}],"predecessor-version":[{"id":6667,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6648\/revisions\/6667"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}