{"id":7003,"date":"2025-06-19T17:33:54","date_gmt":"2025-06-19T12:03:54","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7003"},"modified":"2025-06-19T17:33:56","modified_gmt":"2025-06-19T12:03:56","slug":"design-linked-list","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/design-linked-list\/","title":{"rendered":"Design Linked List"},"content":{"rendered":"\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_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  .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<\/style>\n\n<div class=\"wp_blog_explanation\">\n  <h2>Problem Statement:<\/h2>\n  <p>Design and implement a custom singly linked list class with the following operations:<\/p>\n\n  <h2>Operations:<\/h2>\n  <ul>\n    <li><code>get(index)<\/code>: Return the value of the node at the specified index (0-indexed). If index is invalid, return -1.<\/li>\n    <li><code>addAtHead(val)<\/code>: Insert a node at the beginning of the list.<\/li>\n    <li><code>addAtTail(val)<\/code>: Append a node at the end of the list.<\/li>\n    <li><code>addAtIndex(index, val)<\/code>: Insert a node before the index-th node in the list.<\/li>\n    <li><code>deleteAtIndex(index)<\/code>: Delete the node at the given index.<\/li>\n  <\/ul>\n\n  <h2>Approach:<\/h2>\n  <ul>\n    <li>Use a custom <code>Node<\/code> class that holds <code>val<\/code> and <code>next<\/code> attributes.<\/li>\n    <li>Maintain a <code>head<\/code> pointer to the first node and a <code>size<\/code> to track the list length.<\/li>\n    <li>Each operation ensures index bounds are checked.<\/li>\n    <li>Traverse the list up to the required index using a loop.<\/li>\n    <li>All operations are implemented with time complexity O(n) in the worst case.<\/li>\n  <\/ul>\n<\/div>\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=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"csharp\">C#<\/button>\n  <\/div>\n\n  <!-- CODE BLOCKS GO HERE: Insert each language's final implementation for MyLinkedList inside the respective <pre><code> blocks -->\n  <!-- Example (JavaScript): -->\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\nvar MyLinkedList = function () {\n  this.head = null;\n  this.size = 0;\n};\n\nMyLinkedList.prototype.get = function (index) {\n  if (index < 0 || index >= this.size) return -1;\n  let curr = this.head;\n  for (let i = 0; i < index; i++) curr = curr.next;\n  return curr.val;\n};\n\nMyLinkedList.prototype.addAtHead = function (val) {\n  const newNode = new Node(val);\n  newNode.next = this.head;\n  this.head = newNode;\n  this.size++;\n};\n\nMyLinkedList.prototype.addAtTail = function (val) {\n  const newNode = new Node(val);\n  if (!this.head) this.head = newNode;\n  else {\n    let curr = this.head;\n    while (curr.next) curr = curr.next;\n    curr.next = newNode;\n  }\n  this.size++;\n};\n\nMyLinkedList.prototype.addAtIndex = function (index, val) {\n  if (index < 0 || index > this.size) return;\n  if (index === 0) return this.addAtHead(val);\n  if (index === this.size) return this.addAtTail(val);\n  const newNode = new Node(val);\n  let curr = this.head;\n  for (let i = 0; i < index - 1; i++) curr = curr.next;\n  newNode.next = curr.next;\n  curr.next = newNode;\n  this.size++;\n};\n\nMyLinkedList.prototype.deleteAtIndex = function (index) {\n  if (index < 0 || index >= this.size) return;\n  if (index === 0) this.head = this.head.next;\n  else {\n    let curr = this.head;\n    for (let i = 0; i < index - 1; i++) curr = curr.next;\n    curr.next = curr.next.next;\n  }\n  this.size--;\n};\n    <\/code><\/pre>\n  <\/div>\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">typedef struct Node {\n      int val;\n      struct Node* next;\n  } Node;\n  \n  typedef struct {\n      Node* head;\n      int size;\n  } MyLinkedList;\n  \n  MyLinkedList* myLinkedListCreate() {\n      MyLinkedList* obj = (MyLinkedList*)malloc(sizeof(MyLinkedList));\n      obj->head = NULL;\n      obj->size = 0;\n      return obj;\n  }\n  \n  int myLinkedListGet(MyLinkedList* obj, int index) {\n      if (index < 0 || index >= obj->size) return -1;\n      Node* curr = obj->head;\n      for (int i = 0; i < index; i++) curr = curr->next;\n      return curr->val;\n  }\n  \n  void myLinkedListAddAtHead(MyLinkedList* obj, int val) {\n      Node* node = (Node*)malloc(sizeof(Node));\n      node->val = val;\n      node->next = obj->head;\n      obj->head = node;\n      obj->size++;\n  }\n  \n  void myLinkedListAddAtTail(MyLinkedList* obj, int val) {\n      Node* node = (Node*)malloc(sizeof(Node));\n      node->val = val;\n      node->next = NULL;\n      if (!obj->head) obj->head = node;\n      else {\n          Node* curr = obj->head;\n          while (curr->next) curr = curr->next;\n          curr->next = node;\n      }\n      obj->size++;\n  }\n  \n  void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {\n      if (index < 0 || index > obj->size) return;\n      if (index == 0) {\n          myLinkedListAddAtHead(obj, val);\n          return;\n      }\n      Node* node = (Node*)malloc(sizeof(Node));\n      node->val = val;\n      Node* curr = obj->head;\n      for (int i = 0; i < index - 1; i++) curr = curr->next;\n      node->next = curr->next;\n      curr->next = node;\n      obj->size++;\n  }\n  \n  void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {\n      if (index < 0 || index >= obj->size) return;\n      Node* temp;\n      if (index == 0) {\n          temp = obj->head;\n          obj->head = obj->head->next;\n      } else {\n          Node* curr = obj->head;\n          for (int i = 0; i < index - 1; i++) curr = curr->next;\n          temp = curr->next;\n          curr->next = temp->next;\n      }\n      free(temp);\n      obj->size--;\n  }\n  \n  void myLinkedListFree(MyLinkedList* obj) {\n      Node* curr = obj->head;\n      while (curr) {\n          Node* temp = curr;\n          curr = curr->next;\n          free(temp);\n      }\n      free(obj);\n  }<\/code><\/pre>\n  <\/div>\n  \n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">class MyLinkedList {\n  private:\n      struct Node {\n          int val;\n          Node* next;\n          Node(int x) : val(x), next(nullptr) {}\n      };\n      Node* head;\n      int size;\n  \n  public:\n      MyLinkedList() : head(nullptr), size(0) {}\n  \n      int get(int index) {\n          if (index < 0 || index >= size) return -1;\n          Node* curr = head;\n          for (int i = 0; i < index; i++) curr = curr->next;\n          return curr->val;\n      }\n  \n      void addAtHead(int val) {\n          Node* node = new Node(val);\n          node->next = head;\n          head = node;\n          size++;\n      }\n  \n      void addAtTail(int val) {\n          Node* node = new Node(val);\n          if (!head) head = node;\n          else {\n              Node* curr = head;\n              while (curr->next) curr = curr->next;\n              curr->next = node;\n          }\n          size++;\n      }\n  \n      void addAtIndex(int index, int val) {\n          if (index < 0 || index > size) return;\n          if (index == 0) return addAtHead(val);\n          if (index == size) return addAtTail(val);\n          Node* node = new Node(val);\n          Node* curr = head;\n          for (int i = 0; i < index - 1; i++) curr = curr->next;\n          node->next = curr->next;\n          curr->next = node;\n          size++;\n      }\n  \n      void deleteAtIndex(int index) {\n          if (index < 0 || index >= size) return;\n          Node* temp;\n          if (index == 0) {\n              temp = head;\n              head = head->next;\n          } else {\n              Node* curr = head;\n              for (int i = 0; i < index - 1; i++) curr = curr->next;\n              temp = curr->next;\n              curr->next = temp->next;\n          }\n          delete temp;\n          size--;\n      }\n  };<\/code><\/pre>\n  <\/div>\n  \n  <!-- Java Implementation -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">class MyLinkedList {\n      private class Node {\n          int val;\n          Node next;\n          Node(int val) { this.val = val; }\n      }\n  \n      private Node head;\n      private int size;\n  \n      public MyLinkedList() {\n          head = null;\n          size = 0;\n      }\n  \n      public int get(int index) {\n          if (index < 0 || index >= size) return -1;\n          Node curr = head;\n          for (int i = 0; i < index; i++) curr = curr.next;\n          return curr.val;\n      }\n  \n      public void addAtHead(int val) {\n          Node node = new Node(val);\n          node.next = head;\n          head = node;\n          size++;\n      }\n  \n      public void addAtTail(int val) {\n          Node node = new Node(val);\n          if (head == null) head = node;\n          else {\n              Node curr = head;\n              while (curr.next != null) curr = curr.next;\n              curr.next = node;\n          }\n          size++;\n      }\n  \n      public void addAtIndex(int index, int val) {\n          if (index < 0 || index > size) return;\n          if (index == 0) addAtHead(val);\n          else if (index == size) addAtTail(val);\n          else {\n              Node node = new Node(val);\n              Node curr = head;\n              for (int i = 0; i < index - 1; i++) curr = curr.next;\n              node.next = curr.next;\n              curr.next = node;\n              size++;\n          }\n      }\n  \n      public void deleteAtIndex(int index) {\n          if (index < 0 || index >= size) return;\n          if (index == 0) head = head.next;\n          else {\n              Node curr = head;\n              for (int i = 0; i < index - 1; i++) curr = curr.next;\n              curr.next = curr.next.next;\n          }\n          size--;\n      }\n  }<\/code><\/pre>\n  <\/div>\n  \n  <!-- Python Implementation -->\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:\n    def __init__(self):\n        self.head = None\n        self.size = 0\n\n    def get(self, index):\n        if index < 0 or index >= self.size:\n            return -1\n        curr = self.head\n        for _ in range(index):\n            curr = curr.next\n        return curr.val\n\n    def addAtHead(self, val):\n        node = Node(val)\n        node.next = self.head\n        self.head = node\n        self.size += 1\n\n    def addAtTail(self, val):\n        node = Node(val)\n        if not self.head:\n            self.head = node\n        else:\n            curr = self.head\n            while curr.next:\n                curr = curr.next\n            curr.next = node\n        self.size += 1\n\n    def addAtIndex(self, index, val):\n        if index < 0 or index > self.size:\n            return\n        if index == 0:\n            self.addAtHead(val)\n        elif index == self.size:\n            self.addAtTail(val)\n        else:\n            node = Node(val)\n            curr = self.head\n            for _ in range(index - 1):\n                curr = curr.next\n            node.next = curr.next\n            curr.next = node\n            self.size += 1\n\n    def deleteAtIndex(self, index):\n        if index < 0 or index >= self.size:\n            return\n        if index == 0:\n            self.head = self.head.next\n        else:\n            curr = self.head\n            for _ in range(index - 1):\n                curr = curr.next\n            curr.next = curr.next.next\n        self.size -= 1\n  <\/code><\/pre>\n<\/div>\n\n  \n  <!-- C# Implementation -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"csharp\">\n    <pre><code class=\"language-csharp\">public class MyLinkedList {\n      private class Node {\n          public int val;\n          public Node next;\n          public Node(int val) {\n              this.val = val;\n          }\n      }\n  \n      private Node head;\n      private int size;\n  \n      public MyLinkedList() {\n          head = null;\n          size = 0;\n      }\n  \n      public int Get(int index) {\n          if (index < 0 || index >= size) return -1;\n          Node curr = head;\n          for (int i = 0; i < index; i++) curr = curr.next;\n          return curr.val;\n      }\n  \n      public void AddAtHead(int val) {\n          Node node = new Node(val);\n          node.next = head;\n          head = node;\n          size++;\n      }\n  \n      public void AddAtTail(int val) {\n          Node node = new Node(val);\n          if (head == null) head = node;\n          else {\n              Node curr = head;\n              while (curr.next != null) curr = curr.next;\n              curr.next = node;\n          }\n          size++;\n      }\n  \n      public void AddAtIndex(int index, int val) {\n          if (index < 0 || index > size) return;\n          if (index == 0) AddAtHead(val);\n          else if (index == size) AddAtTail(val);\n          else {\n              Node node = new Node(val);\n              Node curr = head;\n              for (int i = 0; i < index - 1; i++) curr = curr.next;\n              node.next = curr.next;\n              curr.next = node;\n              size++;\n          }\n      }\n  \n      public void DeleteAtIndex(int index) {\n          if (index < 0 || index >= size) return;\n          if (index == 0) head = head.next;\n          else {\n              Node curr = head;\n              for (int i = 0; i < index - 1; i++) curr = curr.next;\n              curr.next = curr.next.next;\n          }\n          size--;\n      }\n  }<\/code><\/pre>\n  <\/div>\n  <!-- Other language blocks will go here (c, cpp, java, py, csharp) -->\n<\/div>\n\n<script>\n  document.addEventListener('DOMContentLoaded', () => {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n        buttons.forEach(btn => btn.classList.remove('active'));\n        contents.forEach(content => content.classList.remove('active'));\n        button.classList.add('active');\n        document.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 custom singly linked list class with the following operations: Operations: get(index): Return the value of the node at the specified index (0-indexed). If index is invalid, return -1. addAtHead(val): Insert a node at the beginning of the list. addAtTail(val): Append a node at the end of the list. addAtIndex(index,<\/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,811,810,174,172,173],"tags":[],"class_list":["post-7003","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7003","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=7003"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7003\/revisions"}],"predecessor-version":[{"id":7005,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7003\/revisions\/7005"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}