{"id":4992,"date":"2021-09-16T11:50:29","date_gmt":"2021-09-16T11:50:29","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4992"},"modified":"2023-04-23T03:44:42","modified_gmt":"2023-04-23T03:44:42","slug":"implement-a-stack-using-a-singly-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/","title":{"rendered":"Implement a Stack using a Singly Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png\" alt=\"\" \/><\/p>\n<p>A stack is an abstract data type that follows the LIFO (Last In First Out) principle. It is widely used in computer science, programming languages, and operating systems. Implementing a stack using a singly linked list is a common exercise that helps to understand how linked lists work and how they can be used to implement other data structures. In this article, we will learn how to do stack implementation using singly linked list.<\/p>\n<h2>Understanding Stack Implementation Using Singly Linked List<\/h2>\n<p>When implementing a stack using singly linked list, it is essential to consider the standard operations of push, pop, and peek. The push operation involves adding an element to the top of the stack, while the pop operation removes the top element from the stack. The peek operation returns the value of the top element without removing it.<\/p>\n<p>Suppose we insert elements 4, 3, 2, and 1 into the stack using the push operation. In push operation elements are added to the top of the stack. In other words, the last element to be pushed onto the stack becomes the top element, while the first element to be pushed becomes the bottom element. So, when elements are pushed onto the stack in that order, the top of the stack will contain element 1, and the bottom of the stack will contain element 4.<\/p>\n<p>When we want to access the elements in the stack, we can only access them in a LIFO (Last-In-First-Out) manner, meaning that we can only access the top element first, followed by the second-to-top element, and so on. In other words, we must first remove the top element using the pop operation to access the next element in the stack.<\/p>\n<p>So, to access the elements in the order 1, 2, 3, and 4, we would need to pop the elements from the stack in reverse order, starting with 1, then 2, then 3, and finally 4. This is because the top of the stack currently contains element 1, so it must be popped first to access the next element, which is 2. The below image shows how these elements are represented in a singly linked list.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/output-8.png\" alt=\"\" \/><\/p>\n<p>Overall when accessing elements in a stack, we can only access them in reverse order of insertion, starting with the last element pushed onto the stack.<\/p>\n<h3>Approach of Stack Implementation Using Singly Linked List<\/h3>\n<p>The approach and algorithm for implementing a stack using singly linked list involves five main functions: push, pop, peek, isEmpty, and display.<\/p>\n<ol>\n<li>\n<p><strong>push() Function<\/strong><br \/>\nThe push function adds an element to the top of the stack. To implement this function with a singly linked list, we first check if the list is empty. If it is, we make the new node the head of the list. If the list is not empty, we make the next of the new node point to the head of the list and then make the new node the new head of the list.<\/p>\n<\/li>\n<li>\n<p><strong>pop() Function<\/strong><br \/>\nThe pop function removes the topmost element of the stack. To implement this function with a singly linked list, we first check if the list is empty. If it is, we return NULL. If there is only one node in the list, we remove the head and return NULL. If there is more than one node in the list, we create a new node temp and make it point to the head. Then, we make the 2nd node our new head by doing head = head \u2192 next. Finally, we make temp \u2192 next = NULL, and free (temp).<\/p>\n<\/li>\n<li>\n<p><strong>Peek() Function<\/strong><br \/>\nThe peek function returns the top element of the stack without deleting it. To implement this function with a singly linked list, we first check if the list is empty. If it is, we simply exit as there is no data in the list. If the list is not empty, we just return the head \u2192 data, as it is the top element of the stack.<\/p>\n<\/li>\n<li>\n<p><strong>isEmpty() Function<\/strong><br \/>\nThe isEmpty function checks whether the stack is empty or not. To implement this function with a singly linked list, we just check if the head is NULL or not. If the head is NULL, it means that the list is empty, so we will return true. If the head is not NULL, it means that the list is not empty, so we will return false.<\/p>\n<\/li>\n<li>\n<p><strong>display() Function<\/strong><br \/>\nThe display function prints the stack. To implement this function with a singly linked list, we do a list traversal and print the elements of the list one by one.<\/p>\n<\/li>\n<\/ol>\n<h3>Dry Run of Implementing Stack Using Singly Linked List<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-4.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-4.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation of Stack Using Singly Linked List<\/h3>\n<p>Now, let&#8217;s see how to implement stack using singly linked list in C, C++, Java, and Python:<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4993 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_4993 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_4993 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4993 .wpsm_nav-tabs > li.active > a, #tab_container_4993 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4993 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_4993 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_4993 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4993 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4993 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4993 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4993 .wpsm_nav-tabs > li > a:hover , #tab_container_4993 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_4993 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_4993 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_4993 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_4993 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_4993 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_4993 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_4993 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4993 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_4993 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4993 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_4993 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_4993\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4993\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4993_1\" aria-controls=\"tabs_desc_4993_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4993_2\" aria-controls=\"tabs_desc_4993_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4993_3\" aria-controls=\"tabs_desc_4993_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4993_4\" aria-controls=\"tabs_desc_4993_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_4993\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_4993_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n  \r\nstruct node {\r\n    int data;\r\n    struct node *next;\r\n}*top;\r\n \r\n\/*\r\nInitialize an empty stack\r\n*\/\r\nvoid initialize() {\r\n    top = NULL;\r\n} \r\n \r\n\/*\r\nChecks if Stack is empty or not\r\n*\/\r\nint isEmpty() {\r\n    if (top == NULL) \r\n        return 1;\r\n    else\r\n        return 0;\r\n}\r\n \r\n\/*\r\nReturns the top element of Stack\r\n*\/\r\nint peek() {\r\n    return top-&gt;data;\r\n}\r\n \r\n\/* Count stack elements *\/\r\nint getStackSize(struct node *head){\r\n    \/* Input Validation *\/\r\n    if (head == NULL) { \r\n       printf(&quot;Error : Invalid stack pointer !!!&#92;n&quot;);       \r\n       return;  \r\n    }\r\n      \r\n    int length = 0;\r\n    while(head != NULL){\r\n        head = head-&gt;next;\r\n        length++;\r\n    }\r\n    return length;\r\n}\r\n \r\n\/* \r\nPush an Element in Stack \r\n*\/\r\nvoid push(int num) {\r\n    struct node *temp;\r\n    temp =(struct node *)malloc(1*sizeof(struct node));\r\n    temp-&gt;data = num;\r\n     \r\n    if (top == NULL) {\r\n        top = temp;\r\n        top-&gt;next = NULL;\r\n    } else {\r\n        temp-&gt;next = top;\r\n        top = temp;\r\n    }\r\n}\r\n \r\n\/*\r\nPop Operation: Removes Top Element of the Stack\r\n*\/\r\nvoid pop() {\r\n    struct node *temp;\r\n    if (isEmpty(top)) {\r\n        printf(&quot;&#92;nStack is Empty&#92;n&quot;);\r\n        return;\r\n    } else {\r\n        temp = top;\r\n        top = top-&gt;next;\r\n        printf(&quot;Removed  Element : %d&#92;n&quot;, temp-&gt;data);   \r\n        free(temp); \r\n    }\r\n}\r\n \r\n\/*\r\n Prints the linked list representation of a stack  \r\n*\/\r\nvoid printStack(struct node *nodePtr) {\r\n  while (nodePtr != NULL) {\r\n     printf(&quot;%d&quot;, nodePtr-&gt;data);\r\n     nodePtr = nodePtr-&gt;next;\r\n     if(nodePtr != NULL)\r\n         printf(&quot;--&gt;&quot;);\r\n  }\r\n  printf(&quot;&#92;n&quot;);\r\n}\r\n \r\nvoid main() {\r\n   \/* Initialize Stack *\/\r\n   initialize();\r\n   \/* Push Elements in stack *\/\r\n   push(1);\r\n   push(2);\r\n   push(3);\r\n   push(4);\r\n   \/* Prints Size of Stack *\/\r\n   printf(&quot;Stack Size : %d&#92;n&quot;, getStackSize(top));\r\n   \/* Printing top element of Stack *\/\r\n   printf(&quot;&#92;nTop Element : %d&#92;n&quot;, peek());\r\n   \/* Printing Stack *\/\r\n   printf(&quot;Stack as linked List&#92;n&quot;);\r\n   printStack(top);\r\n   \/* Removing elements from stack *\/\r\n   pop();\r\n   pop();\r\n   pop();\r\n   pop();\r\n   pop();\r\n   printStack(top);\r\n    \r\n   return;\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4993_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\n\r\n\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* link;\r\n};\r\n\r\nstruct Node* top;\r\n\r\n\/\/ Using this function we will be pushing elements into the stack\r\nvoid push(int data)\r\n{\r\n\r\n    struct Node* tem;\r\n    tem = new Node();\r\n\r\n    if (!tem)\r\n    {\r\n        cout &lt;&lt; &quot;&#92;nHeap Overflow&quot;;\r\n        exit(1);\r\n    }\r\n\r\n    tem-&gt;data = data;\r\n\r\n    tem-&gt;link = top;\r\n\r\n    top = tem;\r\n}\r\n\r\n\/\/ Using this function we will be checking whether the stack is empty or not\r\nint isEmpty()\r\n{\r\n    return top == NULL;\r\n}\r\n\r\n\/\/ Using this function we will return the top element of the stack\r\nint peek()\r\n{\r\n\r\n    if (!isEmpty())\r\n        return top-&gt;data;\r\n    else\r\n        exit(1);\r\n}\r\n\r\n\/\/ Using this function we will pop the top element of the stack\r\nvoid pop()\r\n{\r\n    struct Node* tem;\r\n\r\n    if (top == NULL)\r\n    {\r\n        cout &lt;&lt; &quot;&#92;nStack Underflow&quot; &lt;&lt; endl;\r\n        exit(1);\r\n    }\r\n    else\r\n    {\r\n        tem = top;\r\n\r\n        top = top-&gt;link;\r\n\r\n        tem-&gt;link = NULL;\r\n\r\n        free(tem);\r\n    }\r\n}\r\n\r\n\/\/ this function will be used to display the items of the stack\r\nvoid display()\r\n{\r\n    struct Node* tem;\r\n\r\n    if (top == NULL)\r\n    {\r\n        cout &lt;&lt; &quot;&#92;nStack Underflow&quot;;\r\n        exit(1);\r\n    }\r\n    else\r\n    {\r\n        tem = top;\r\n        while (tem != NULL)\r\n        {\r\n\r\n            cout &lt;&lt; tem-&gt;data &lt;&lt; &quot;-&gt; &quot;;\r\n\r\n            tem = tem-&gt;link;\r\n        }\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    push(4);\r\n    push(3);\r\n    push(2);\r\n    push(1);\r\n    display();\r\n\r\n    cout &lt;&lt; &quot;&#92;nTop element is &quot;&lt;&lt; peek() &lt;&lt; endl;\r\n\r\n    pop();\r\n    pop();\r\n\r\n    cout&lt;&lt;&quot;Stack after popping 2 times &#92;n&quot;;\r\n    display();\r\n\r\n    cout &lt;&lt; &quot;&#92;nTop element is &quot;&lt;&lt; peek() &lt;&lt; endl;\r\n        \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4993_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nimport static java.lang.System.exit;\r\n\r\nclass StackUsingLinkedlist {\r\n\r\n    private class Node {\r\n\r\n        int data;\r\n        Node link; \r\n    }\r\n\r\n    Node top;\r\n\r\n    StackUsingLinkedlist()\r\n    {\r\n        this.top = null;\r\n    }\r\n\r\n    \/\/ Using this function we will be pushing elements into the stack\r\n    public void push(int x) \r\n    {\r\n\r\n        Node temp = new Node();\r\n\r\n        if (temp == null) {\r\n            System.out.print(&quot;&#92;nHeap Overflow&quot;);\r\n            return;\r\n        }\r\n\r\n        temp.data = x;\r\n\r\n        temp.link = top;\r\n\r\n        top = temp;\r\n    }\r\n\r\n    \/\/ Using this function we will be checking whether the stack is empty or not\r\n    public boolean isEmpty()\r\n    {\r\n        return top == null;\r\n    }\r\n\r\n    \/\/ using this function we will return the top element of the stack\r\n    public int peek()\r\n    {\r\n\r\n        if (!isEmpty()) {\r\n            return top.data;\r\n        }\r\n        else {\r\n            System.out.println(&quot;Stack is empty&quot;);\r\n            return -1;\r\n        }\r\n    }\r\n\r\n    \/\/ Using this function we will pop the top element of the stack\r\n    public void pop() \r\n    {\r\n\r\n        if (top == null) {\r\n            System.out.print(&quot;&#92;nStack Underflow&quot;);\r\n            return;\r\n        }\r\n\r\n        top = (top).link;\r\n    }\r\n\r\n    \/\/ this function will be used to display the items of the stack\r\n    public void display()\r\n    {\r\n\r\n        if (top == null) {\r\n            System.out.printf(&quot;&#92;nStack Underflow&quot;);\r\n            exit(1);\r\n        }\r\n        else {\r\n            Node temp = top;\r\n            while (temp != null) {\r\n\r\n                System.out.printf(&quot;%d-&gt;&quot;, temp.data);\r\n\r\n                temp = temp.link;\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\npublic class PrepBytes {\r\n    public static void main(String[] args)\r\n    {\r\n\r\n        StackUsingLinkedlist stk = new StackUsingLinkedlist();\r\n\r\n        stk.push(4);\r\n        stk.push(3);\r\n        stk.push(2);\r\n        stk.push(1);\r\n\r\n        stk.display();\r\n\r\n        System.out.printf(&quot;&#92;nTop element is %d&#92;n&quot;, stk.peek());\r\n        System.out.println(&quot;Stack after popping 2 times&quot;);\r\n        stk.pop();\r\n        stk.pop();\r\n\r\n        stk.display();\r\n\r\n        System.out.printf(&quot;&#92;nTop element is %d&#92;n&quot;, stk.peek());\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4993_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n    \r\n    def __init__(self,data):\r\n        self.data = data\r\n        self.next = None\r\n    \r\nclass Stack:\r\n    \r\n    def __init__(self):\r\n        self.head = None\r\n    \r\n    def isempty(self):\r\n        if self.head == None:\r\n            return True\r\n        else:\r\n            return False\r\n    \r\n    def push(self,data):\r\n        \r\n        if self.head == None:\r\n            self.head=Node(data)\r\n            \r\n        else:\r\n            newnode = Node(data)\r\n            newnode.next = self.head\r\n            self.head = newnode\r\n    \r\n    def pop(self):\r\n        \r\n        if self.isempty():\r\n            return None\r\n            \r\n        else:\r\n\r\n            poppednode = self.head\r\n            self.head = self.head.next\r\n            poppednode.next = None\r\n            return poppednode.data\r\n    \r\n    def peek(self):\r\n        \r\n        if self.isempty():\r\n            return None\r\n            \r\n        else:\r\n            return self.head.data\r\n    \r\n    def display(self):\r\n        \r\n        iternode = self.head\r\n        if self.isempty():\r\n            print(&quot;Stack Underflow&quot;)\r\n        \r\n        else:\r\n            \r\n            while(iternode != None):\r\n                \r\n                print(iternode.data,&quot;-&gt;&quot;,end = &quot; &quot;)\r\n                iternode = iternode.next\r\n            return\r\n        \r\nMyStack = Stack()\r\n\r\nMyStack.push(4)\r\nMyStack.push(3)\r\nMyStack.push(2)\r\nMyStack.push(1)\r\n\r\nMyStack.display()\r\n\r\nprint(&quot;&#92;nTop element is &quot;,MyStack.peek())\r\n\r\nMyStack.pop()\r\nMyStack.pop()\r\n\r\nprint(&quot;Stack after poping 2 times&quot;)\r\nMyStack.display()\r\n\r\nprint(&quot;&#92;nTop element is &quot;, MyStack.peek())\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4993 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_4993 a\"),jQuery(\"#tab-content_4993\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Output:<\/strong><\/p>\n<pre><code>1->2->3->4->\nTop element is 1\nStack after popping 2 times\n3->4->\nTop element is 3<\/code><\/pre>\n<h3>Complexity Analysis of Stack Implementation Using Singly Linked List<\/h3>\n<p>Let&#8217;s analyze the time and space complexity of each operation in the implementation of a stack using linked list.<\/p>\n<ol>\n<li>\n<p><strong>push() Operation:<\/strong><br \/>\nSince we always add a new element at the top of the stack, this operation takes constant time. We only need to allocate memory for the new node, set its data, and update the &quot;next&quot; pointer to the current top of the stack.<\/p>\n<p><strong>Time complexity:<\/strong> O(1)<br \/>\n<strong>Space complexity:<\/strong> O(1)<\/p>\n<p>We only allocate memory for the new node, so the space complexity of the push operation is constant.<\/p>\n<\/li>\n<li>\n<p><strong>pop() Operation:<\/strong><br \/>\nSince we always remove the top element of the stack, this operation takes constant time. We only need to update the &quot;top&quot; pointer to the next node and free the memory of the removed node.<\/p>\n<p><strong>Time complexity:<\/strong> O(1)<br \/>\n<strong>Space complexity:<\/strong> O(1)<\/p>\n<p>We only remove a node from the stack, so the space complexity of the pop operation is constant.<\/p>\n<\/li>\n<li>\n<p>peek() Operation:<br \/>\nThis operation only returns the value of the top element, so it takes constant time.<\/p>\n<p><strong>Time complexity:<\/strong> O(1)<br \/>\n<strong>Space complexity:<\/strong> O(1)<\/p>\n<p>We don&#8217;t need to allocate or remove any memory, so the space complexity of the peek operation is constant.<\/p>\n<\/li>\n<li>\n<p>isEmpty() Operation:<br \/>\nThis operation only checks if the &quot;top&quot; pointer is null or not, so it takes constant time.<\/p>\n<p><strong>Time complexity:<\/strong> O(1)<br \/>\n<strong>Space complexity:<\/strong> O(1)<\/p>\n<p>We don&#8217;t need to allocate or remove any memory, so the space complexity of the isEmpty operation is constant.<\/p>\n<\/li>\n<li>\n<p>getStackSize() Operation:<br \/>\nWe need to traverse the whole linked list to count the number of nodes, so the time complexity of this operation is linear to the size of the stack.<\/p>\n<p><strong>Time complexity:<\/strong> O(n)<br \/>\n<strong>Space complexity:<\/strong> O(1)<\/p>\n<p>We don&#8217;t allocate or remove any extra memory, so the space complexity of the getStackSize operation is constant.<\/p>\n<\/li>\n<\/ol>\n<p>Overall, the time and space complexity of operations of a stack using singly linked list are efficient and provide good performance.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, the implementation of a stack using singly linked list is an efficient way to perform various stack operations such as push, pop, peek, and check if the stack is empty. The linked list implementation has a time complexity of O(1) for insertion and deletion operations, and the space complexity increases linearly with the number of elements in the stack. Overall, the stack implementation using singly linked list is a practical and efficient way to perform stack operations. If you want to solve more questions on Linked List, which is curated by our expert mentors at PrepBytes, you can follow this link Linked List.<\/p>\n<h2>FAQs Related to Stack<\/h2>\n<p>Here are some frequently asked questions related to stack implementation using singly linked lists.<\/p>\n<p><strong>Q1: How do you push an element into an implementation of stack using singly linked list?<\/strong><br \/>\n<strong>Answer:<\/strong> To push an element into an implementation of stack using singly linked list, you need to create a new node and set its data field to the value you want to push. Then, you set its next field to the current head of the linked list and update the head of the linked list to point to the new node.<\/p>\n<p><strong>Q2: How do you pop an element from the implementation of stack using singly linked list?<\/strong><br \/>\n<strong>Answer:<\/strong> To pop an element from the implementation of stack using singly linked list, you simply remove the head of the linked list and update the head of the linked list to point to the next element in the stack.<\/p>\n<p><strong>Q3: How do you handle stack overflow in a stack implementation using singly linked list?<\/strong><br \/>\n<strong>Answer:<\/strong> Since a linked list can grow dynamically, stack overflow is not typically a concern when implementing a stack using singly linked list.<\/p>\n<p><strong>Q4: How do you handle memory leaks in a stack implementation using singly linked list?<\/strong><br \/>\n<strong>Answer:<\/strong> To avoid memory leaks in a stack implementation using singly linked list, you should always free the memory allocated for each node when it is removed from the stack.<\/p>\n<p><strong>Q5: How do you handle concurrent access to a stack implementation using singly linked list?<\/strong><br \/>\n<strong>Answer:<\/strong> To handle concurrent access to a stack implemented using singly linked list, you should use a locking mechanism such as a mutex to ensure that only one thread can access the stack at a time.<\/p>\n<p><strong>Q6: How do you handle data corruption in an implementation of a stack using singly linked list?<\/strong><br \/>\n<strong>Answer:<\/strong> Data corruption can occur if the pointers in the linked list become invalid due to memory errors or other issues. To avoid data corruption in the implementation of stack using singly linked list, you should always check the validity of pointers before dereferencing them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A stack is an abstract data type that follows the LIFO (Last In First Out) principle. It is widely used in computer science, programming languages, and operating systems. Implementing a stack using a singly linked list is a common exercise that helps to understand how linked lists work and how they can be used to [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-4992","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Implement a stack using a singly linked list | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"This blog explains the most efficient approach to implement a stack using a singly linked list. This is an important concept when it comes to coding interviews.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implement a stack using a singly linked list | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"This blog explains the most efficient approach to implement a stack using a singly linked list. This is an important concept when it comes to coding interviews.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-16T11:50:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-23T03:44:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Implement a Stack using a Singly Linked List\",\"datePublished\":\"2021-09-16T11:50:29+00:00\",\"dateModified\":\"2023-04-23T03:44:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/\"},\"wordCount\":1572,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/\",\"name\":\"Implement a stack using a singly linked list | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png\",\"datePublished\":\"2021-09-16T11:50:29+00:00\",\"dateModified\":\"2023-04-23T03:44:42+00:00\",\"description\":\"This blog explains the most efficient approach to implement a stack using a singly linked list. This is an important concept when it comes to coding interviews.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Implement a Stack using a Singly Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implement a stack using a singly linked list | Linked List | Prepbytes","description":"This blog explains the most efficient approach to implement a stack using a singly linked list. This is an important concept when it comes to coding interviews.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Implement a stack using a singly linked list | Linked List | Prepbytes","og_description":"This blog explains the most efficient approach to implement a stack using a singly linked list. This is an important concept when it comes to coding interviews.","og_url":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-16T11:50:29+00:00","article_modified_time":"2023-04-23T03:44:42+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Implement a Stack using a Singly Linked List","datePublished":"2021-09-16T11:50:29+00:00","dateModified":"2023-04-23T03:44:42+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/"},"wordCount":1572,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/","name":"Implement a stack using a singly linked list | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png","datePublished":"2021-09-16T11:50:29+00:00","dateModified":"2023-04-23T03:44:42+00:00","description":"This blog explains the most efficient approach to implement a stack using a singly linked list. This is an important concept when it comes to coding interviews.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001154674-Article_145.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/implement-a-stack-using-a-singly-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Implement a Stack using a Singly Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=4992"}],"version-history":[{"count":8,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4992\/revisions"}],"predecessor-version":[{"id":15833,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4992\/revisions\/15833"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}