{"id":621,"date":"2025-07-15T10:42:04","date_gmt":"2025-07-15T05:12:04","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=621"},"modified":"2025-07-15T10:42:06","modified_gmt":"2025-07-15T05:12:06","slug":"understanding-singly-linked-list-in-python","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/","title":{"rendered":"Understanding Singly Linked List\u00a0in Python | Complete\u00a0Explanation and\u00a0Implementation\u00a0Guide"},"content":{"rendered":"\n<p>A singly linked list is a key data structure in computer science, often used for dynamic memory allocation and for cases where frequent insertions and deletions are needed. Let\u2019s explore the concept thoroughly by breaking down the step-by-step implementation of a singly linked list (SLL) in Python. We will cover all foundational aspects, from how nodes are defined, to adding (append\/insert), removing (delete), and traversing (printing) elements.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-f7091b60-d707-46ee-a4bb-e8bcb32f3c0b\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#0-1-defining-a-node\" style=\"\">1. Defining a Node<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#1-2-the-singly-linked-list-class\" style=\"\">2. The Singly Linked List Class<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#2-3-appending-a-node-add-to-end\" style=\"\">3. Appending a Node (Add to End)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#3-4-traversing-the-list-printing-all-nodes\" style=\"\">4. Traversing the List (Printing All Nodes)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#4-5-inserting-a-node-at-a-specific-position\" style=\"\">5. Inserting a Node at a Specific Position<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#5-6-deleting-the-head-node-first-node\" style=\"\">6. Deleting the Head Node (First Node)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#6-7-deleting-a-node-by-value\" style=\"\">7. Deleting a Node by Value<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#7-8-demonstration-using-the-singly-linked-list\" style=\"\">8. Demonstration: Using the Singly Linked List<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#8-time-and-space-complexity-analysis\" style=\"\">Time and Space Complexity Analysis<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-singly-linked-list-in-python\/#9-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-1-defining-a-node\">1. Defining a Node<\/h2>\n\n\n\n<p>A singly linked list is composed of basic building blocks called&nbsp;<strong>nodes<\/strong>. Each node holds two important pieces of information:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Value<\/strong>: The actual data stored in the node.<\/li>\n\n\n\n<li><strong>Next<\/strong>: A reference (or pointer) to the next node in the list.<\/li>\n<\/ul>\n\n\n\n<p>Here\u2019s how you define a node in Python:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Node:\n    def __init__(self, val=None):\n        self.val = val          # Stores the value of this node\n        self.next = None        # Points to the next node (initially None)\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Node<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">__init__<\/span><span style=\"color: #E1E4E8\">(self, val<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #79B8FF\">None<\/span><span style=\"color: #E1E4E8\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.val <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> val          <\/span><span style=\"color: #6A737D\"># Stores the value of this node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.next <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #6A737D\"># Points to the next node (initially None)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p>Every time a new node is created, it is initialized with a value (<code>val<\/code>) and a pointer to the next node (<code>next<\/code>). By default, the next pointer is set to&nbsp;<code>None<\/code>&nbsp;because when a node is created, it does not yet link to another node.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-2-the-singly-linked-list-class\">2. The Singly Linked List Class<\/h2>\n\n\n\n<p>The&nbsp;<strong>SinglyLinkedList<\/strong>&nbsp;class is responsible for managing the linked list. It maintains a reference to the first node (called the&nbsp;<strong>head<\/strong>) and provides the functionality for various operations.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class SinglyLinkedList:\n    def __init__(self):\n        self.head = None    # Head points to the first node in the list\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">SinglyLinkedList<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">__init__<\/span><span style=\"color: #E1E4E8\">(self):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #6A737D\"># Head points to the first node in the list<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p>At initialization, the list is empty, so&nbsp;<code>self.head<\/code>&nbsp;is set to&nbsp;<code>None<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-3-appending-a-node-add-to-end\">3. Appending a Node (Add to End)<\/h2>\n\n\n\n<p>The&nbsp;<strong>append<\/strong>&nbsp;operation adds a new node at the end (tail) of the list. This requires traversing the whole list until the last node is found, then adding the new node after it.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def append(self, data):\n    new_node = Node(data)      # Create a new node with the given value\n    if not self.head:          # If the list is empty, set head to new node\n        self.head = new_node\n    else:\n        current = self.head\n        while current.next is not None:   # Traverse to the last node\n            current = current.next\n        current.next = new_node           # Link the last node to the new node\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">append<\/span><span style=\"color: #E1E4E8\">(self, data):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    new_node <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> Node(data)      <\/span><span style=\"color: #6A737D\"># Create a new node with the given value<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">not<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head:          <\/span><span style=\"color: #6A737D\"># If the list is empty, set head to new node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> new_node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        current <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> current.next <\/span><span style=\"color: #F97583\">is<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">not<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><span style=\"color: #E1E4E8\">:   <\/span><span style=\"color: #6A737D\"># Traverse to the last node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            current <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> current.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        current.next <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> new_node           <\/span><span style=\"color: #6A737D\"># Link the last node to the new node<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the list is empty (<code>self.head<\/code>\u00a0is\u00a0<code>None<\/code>), the new node becomes the head.<\/li>\n\n\n\n<li>Otherwise, we traverse the list to the last node (the one whose\u00a0<code>next<\/code>\u00a0points to\u00a0<code>None<\/code>), then set its\u00a0<code>next<\/code>\u00a0pointer to the newly created node.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-4-traversing-the-list-printing-all-nodes\">4. Traversing the List (Printing All Nodes)<\/h2>\n\n\n\n<p><strong>Traversal<\/strong>&nbsp;means visiting each node in the sequence and performing an action\u2014here, we\u2019ll print all the values stored in the nodes.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def traverse(self):\n    if not self.head:\n        print(&quot;Singly Linked List is empty&quot;)\n    else:\n        current = self.head\n        while current is not None:\n            print(current.val, end=&quot; &quot;)\n            current = current.next\n        print()\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">traverse<\/span><span style=\"color: #E1E4E8\">(self):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">not<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">print<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">&quot;Singly Linked List is empty&quot;<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        current <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> current <\/span><span style=\"color: #F97583\">is<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">not<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #79B8FF\">print<\/span><span style=\"color: #E1E4E8\">(current.val, <\/span><span style=\"color: #FFAB70\">end<\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #9ECBFF\">&quot; &quot;<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            current <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> current.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">print<\/span><span style=\"color: #E1E4E8\">()<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the list is empty, it notifies the user.<\/li>\n\n\n\n<li>Otherwise, it starts at the head and follows the\u00a0<code>next<\/code>\u00a0pointers until the end, printing each value.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-5-inserting-a-node-at-a-specific-position\">5. Inserting a Node at a Specific Position<\/h2>\n\n\n\n<p>To insert a node at a particular position in the list, we must handle two scenarios: inserting at the head and inserting somewhere else within or at the end of the list.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def insert_at(self, data, position):\n    new_node = Node(data)\n    if position == 0:\n        new_node.next = self.head\n        self.head = new_node\n    else:\n        current = self.head\n        prev_node = None\n        count = 0\n        while current is not None and count &lt; position:\n            prev_node = current\n            current = current.next\n            count += 1\n        new_node.next = current\n        if prev_node:\n            prev_node.next = new_node\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">insert_at<\/span><span style=\"color: #E1E4E8\">(self, data, position):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    new_node <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> Node(data)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> position <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        new_node.next <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> new_node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        current <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        prev_node <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        count <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> current <\/span><span style=\"color: #F97583\">is<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">not<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">and<\/span><span style=\"color: #E1E4E8\"> count <\/span><span style=\"color: #F97583\">&lt;<\/span><span style=\"color: #E1E4E8\"> position:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            prev_node <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> current<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            current <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> current.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            count <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        new_node.next <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> current<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> prev_node:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            prev_node.next <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> new_node<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the insertion position is zero, the new node becomes the head. The old head is moved to the second position.<\/li>\n\n\n\n<li>For other positions, we traverse to the node just before the desired position, link the new node into the chain, and update pointers accordingly.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-6-deleting-the-head-node-first-node\">6. Deleting the Head Node (First Node)<\/h2>\n\n\n\n<p>Removing the first node simply means updating the head to point to the second node.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def delete_head(self):\n    if not self.head:\n        print(&quot;Cannot delete. Singly Linked List is already empty&quot;)\n    else:\n        self.head = self.head.next  # The second node (or None) becomes the new head\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">delete_head<\/span><span style=\"color: #E1E4E8\">(self):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">not<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">print<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">&quot;Cannot delete. Singly Linked List is already empty&quot;<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head.next  <\/span><span style=\"color: #6A737D\"># The second node (or None) becomes the new head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the list is empty, a notification is printed.<\/li>\n\n\n\n<li>Otherwise, the head is updated to the next node, effectively removing the first node.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-7-deleting-a-node-by-value\">7. Deleting a Node by Value<\/h2>\n\n\n\n<p>This operation deletes a node containing a specific value. We need to track both the current node and its predecessor (previous node).<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def delete(self, val):\n    temp = self.head\n    if temp and temp.val == val:\n        self.head = temp.next\n        return\n\n    prev = None\n    found = False\n\n    while temp is not None:\n        if temp.val == val:\n            found = True\n            break\n        prev = temp\n        temp = temp.next\n\n    if found:\n        prev.next = temp.next\n    else:\n        print(&quot;Node not found&quot;)\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">delete<\/span><span style=\"color: #E1E4E8\">(self, val):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    temp <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> temp <\/span><span style=\"color: #F97583\">and<\/span><span style=\"color: #E1E4E8\"> temp.val <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> val:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.head <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> temp.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    prev <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    found <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">False<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> temp <\/span><span style=\"color: #F97583\">is<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #F97583\">not<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">None<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> temp.val <\/span><span style=\"color: #F97583\">==<\/span><span style=\"color: #E1E4E8\"> val:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            found <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">break<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        prev <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> temp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        temp <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> temp.next<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> found:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        prev.next <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> temp.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #79B8FF\">print<\/span><span style=\"color: #E1E4E8\">(<\/span><span style=\"color: #9ECBFF\">&quot;Node not found&quot;<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the value is in the head node, we remove it by updating the head.<\/li>\n\n\n\n<li>Otherwise, we search for the node with the target value. If found, it is bypassed by updating the previous node\u2019s\u00a0<code>next<\/code>\u00a0pointer.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-8-demonstration-using-the-singly-linked-list\">8. Demonstration: Using the Singly Linked List<\/h2>\n\n\n\n<p>Let\u2019s see how each operation behaves step by step.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sll = SinglyLinkedList()\n\n# Append nodes\nsll.append(100)\nsll.append(200)\nsll.append(50)\nsll.append(20)\nsll.traverse()      # Output: 100 200 50 20\n\n# Insert nodes\nsll.insert_at(43, 0)      # Insert 43 at the beginning\nsll.traverse()            # Output: 43 100 200 50 20\n\nsll.insert_at(400, 3)     # Insert 400 at position 3\nsll.traverse()            # Output: 43 100 200 400 50 20\n\n# Delete head node\nsll.delete_head()\nsll.traverse()            # Output: 100 200 400 50 20\n\n# Attempt to delete a non-existent node\nsll.delete(2000)          # Value not found\nsll.traverse()            # Output: 100 200 400 50 20 (unchanged)\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #E1E4E8\">sll <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> SinglyLinkedList()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A737D\"># Append nodes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.append(<\/span><span style=\"color: #79B8FF\">100<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.append(<\/span><span style=\"color: #79B8FF\">200<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.append(<\/span><span style=\"color: #79B8FF\">50<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.append(<\/span><span style=\"color: #79B8FF\">20<\/span><span style=\"color: #E1E4E8\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.traverse()      <\/span><span style=\"color: #6A737D\"># Output: 100 200 50 20<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A737D\"># Insert nodes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.insert_at(<\/span><span style=\"color: #79B8FF\">43<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">0<\/span><span style=\"color: #E1E4E8\">)      <\/span><span style=\"color: #6A737D\"># Insert 43 at the beginning<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.traverse()            <\/span><span style=\"color: #6A737D\"># Output: 43 100 200 50 20<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.insert_at(<\/span><span style=\"color: #79B8FF\">400<\/span><span style=\"color: #E1E4E8\">, <\/span><span style=\"color: #79B8FF\">3<\/span><span style=\"color: #E1E4E8\">)     <\/span><span style=\"color: #6A737D\"># Insert 400 at position 3<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.traverse()            <\/span><span style=\"color: #6A737D\"># Output: 43 100 200 400 50 20<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A737D\"># Delete head node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.delete_head()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.traverse()            <\/span><span style=\"color: #6A737D\"># Output: 100 200 400 50 20<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A737D\"># Attempt to delete a non-existent node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.delete(<\/span><span style=\"color: #79B8FF\">2000<\/span><span style=\"color: #E1E4E8\">)          <\/span><span style=\"color: #6A737D\"># Value not found<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">sll.traverse()            <\/span><span style=\"color: #6A737D\"># Output: 100 200 400 50 20 (unchanged)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>Walkthrough of the Outputs:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Appending<\/strong>: Nodes are always added at the end. After successive appends:\u00a0<code>100 \u2192 200 \u2192 50 \u2192 20<\/code><\/li>\n\n\n\n<li><strong>Traversing<\/strong>: Prints all the elements in order.<\/li>\n\n\n\n<li><strong>Insertion<\/strong>: You can insert at the beginning (position 0) or any other position. E.g. inserting\u00a0<code>43<\/code>\u00a0at position one gives:\u00a0<code>43 \u2192 100 \u2192 200 \u2192 50 \u2192 20<\/code><\/li>\n\n\n\n<li><strong>Deletion by Head<\/strong>: Removes the first node.<\/li>\n\n\n\n<li><strong>Deletion by Value<\/strong>: Searches and deletes the first node with the matching value, if found.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-time-and-space-complexity-analysis\">Time and Space Complexity Analysis<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operation<\/th><th>Time Complexity<\/th><th>Space Complexity<\/th><\/tr><\/thead><tbody><tr><td>Append<\/td><td>O(N)<\/td><td>O(1)<\/td><\/tr><tr><td>Traverse<\/td><td>O(N)<\/td><td>O(1)<\/td><\/tr><tr><td>Insert at<\/td><td>O(N)<\/td><td>O(1)<\/td><\/tr><tr><td>Delete Head<\/td><td>O(1)<\/td><td>O(1)<\/td><\/tr><tr><td>Delete by Value<\/td><td>O(N)<\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>N<\/strong>\u00a0is the number of nodes in the linked list.<\/li>\n\n\n\n<li>Most operations require traversal (linear time), except deletion\/insertion at the head (constant time).<\/li>\n\n\n\n<li>All operations use only a constant amount of extra space.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-summary\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Linked Lists<\/strong>\u00a0organize data in nodes, which are linked using pointers.<\/li>\n\n\n\n<li>Operations like insertion, deletion, and traversal are commonly performed in linked lists.<\/li>\n\n\n\n<li>Linked lists are especially useful when you need fast insertions and deletions from the list, or when the size of your data changes frequently.<\/li>\n\n\n\n<li>Unlike arrays, linked lists do not require contiguous memory and can grow or shrink as needed.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A singly linked list is a key data structure in computer science, often used for dynamic memory allocation and for cases where frequent insertions and deletions are needed. Let\u2019s explore the concept thoroughly by breaking down the step-by-step implementation of a singly linked list (SLL) in Python. We will cover all foundational aspects, from how<\/p>\n","protected":false},"author":1,"featured_media":623,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[29],"class_list":{"0":"post-621","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-singly-linked-list"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/singly-linked-list-in-python-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/621\/revisions\/624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/623"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}