{"id":690,"date":"2025-07-18T14:55:22","date_gmt":"2025-07-18T09:25:22","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=690"},"modified":"2025-07-18T14:55:23","modified_gmt":"2025-07-18T09:25:23","slug":"understanding-doubly-linked-lists-complete-guide-with-real-life-examples","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/","title":{"rendered":"Understanding Doubly Linked Lists: Complete Guide with Real-Life Examples"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"0-what-is-a-doubly-linked-list\">What is a Doubly Linked List?<\/h2>\n\n\n\n<p>A&nbsp;<strong>doubly linked list<\/strong>&nbsp;is a type of linked list where each node contains data and&nbsp;<strong>two pointers<\/strong>&nbsp;&#8211; one pointing to the next node and another pointing to the previous node. Think of it as a chain where each link is connected to both the link before it and the link after it, allowing you to move in both directions!<\/p>\n\n\n\n<p>Unlike a regular (singly) linked list where you can only move forward, a doubly linked list lets you traverse both&nbsp;<strong>forward and backward<\/strong>&nbsp;through the data.<\/p>\n\n\n<div class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-b4b1470f-6157-4fa8-9da6-cb3dade8d539\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"false\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><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\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 \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#0-what-is-a-doubly-linked-list\" style=\"\">What is a Doubly Linked List?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#1-real-life-examples\" style=\"\">Real-Life Examples<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#2-1-music-playlist-\" style=\"\">1.\u00a0Music Playlist<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#3-2-browser-history-\" style=\"\">2.\u00a0Browser History<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#4-3-photo-gallery-\" style=\"\">3.\u00a0Photo Gallery<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#5-4-train-compartments-\" style=\"\">4.\u00a0Train Compartments<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#6-memory-utilization-compared-to-arrayslists\" style=\"\">Memory Utilization Compared to Arrays\/Lists<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#7-memory-structure-comparison\" style=\"\">Memory Structure Comparison<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#8-memory-usage-analysis\" style=\"\">Memory Usage Analysis<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#9-pros-and-cons\" style=\"\">Pros and Cons<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#10-advantages-\" style=\"\">Advantages<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#11-disadvantages-\" style=\"\">Disadvantages<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#12-complete-code-walkthrough\" style=\"\">Complete Code Walkthrough<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#13-node-structure\" style=\"\">Node Structure<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#14-basic-operations\" style=\"\">Basic Operations<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#15-1-insert-at-head-\" style=\"\">1.\u00a0Insert at Head<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#16-2-insert-at-end-append-\" style=\"\">2.\u00a0Insert at End (Append)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#17-3-insert-at-position-\" style=\"\">3.\u00a0Insert at Position<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#18-4-traversal-operations-\" style=\"\">4.\u00a0Traversal Operations<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#19-when-to-use-doubly-linked-lists\" style=\"\">When to Use Doubly Linked Lists<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#20-use-when-\" style=\"\">Use When:<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#21-dont-use-when-\" style=\"\">Don&#8217;t Use When:<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/understanding-doubly-linked-lists-complete-guide-with-real-life-examples\/#22-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"1-real-life-examples\">Real-Life Examples<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-1-music-playlist-\">1.&nbsp;<strong>Music Playlist<\/strong><\/h3>\n\n\n\n<p>Your music app&#8217;s playlist is like a doubly linked list! You can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Move to the&nbsp;<strong>next song<\/strong>&nbsp;(forward traversal)<\/li>\n\n\n\n<li>Go back to the&nbsp;<strong>previous song<\/strong>&nbsp;(backward traversal)<\/li>\n\n\n\n<li>Add songs at the&nbsp;<strong>beginning, middle, or end<\/strong><\/li>\n\n\n\n<li>Remove songs from anywhere in the playlist<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-2-browser-history-\">2.&nbsp;<strong>Browser History<\/strong><\/h3>\n\n\n\n<p>When you browse the internet:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Forward button<\/strong>&nbsp;takes you to the next page you visited<\/li>\n\n\n\n<li><strong>Back button<\/strong>&nbsp;takes you to the previous page<\/li>\n\n\n\n<li>You can navigate in both directions through your browsing history<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-3-photo-gallery-\">3.&nbsp;<strong>Photo Gallery<\/strong><\/h3>\n\n\n\n<p>In your phone&#8217;s photo gallery:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Swipe&nbsp;<strong>right<\/strong>&nbsp;to see the next photo<\/li>\n\n\n\n<li>Swipe&nbsp;<strong>left<\/strong>&nbsp;to see the previous photo<\/li>\n\n\n\n<li>You can navigate through photos in both directions<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-4-train-compartments-\">4.&nbsp;<strong>Train Compartments<\/strong><\/h3>\n\n\n\n<p>A train with connected compartments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can walk&nbsp;<strong>forward<\/strong>&nbsp;through compartments<\/li>\n\n\n\n<li>You can walk&nbsp;<strong>backward<\/strong>&nbsp;through compartments<\/li>\n\n\n\n<li>Each compartment is connected to the one before and after it<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-memory-utilization-compared-to-arrayslists\">Memory Utilization Compared to Arrays\/Lists<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-memory-structure-comparison\">Memory Structure Comparison<\/h3>\n\n\n\n<p><strong>Array\/List:<\/strong><\/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:#D4D4D4;--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:#1E1E1E\"><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=\"Memory: [10][20][30][40][50]\n        Continuous memory blocks\" style=\"color:#D4D4D4;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 dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Memory: [<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #B5CEA8\">40<\/span><span style=\"color: #D4D4D4\">][<\/span><span style=\"color: #B5CEA8\">50<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        Continuous memory blocks<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>Doubly Linked List:<\/strong><\/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:#D4D4D4;--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:#1E1E1E\"><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=\"Memory: [prev|10|next] -&gt; [prev|20|next] -&gt; [prev|30|next]\n        Scattered memory locations\" style=\"color:#D4D4D4;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 dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Memory: [prev|<\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\">|<\/span><span style=\"color: #DCDCAA\">next<\/span><span style=\"color: #D4D4D4\">] <\/span><span style=\"color: #F44747\">-&gt;<\/span><span style=\"color: #D4D4D4\"> [prev|<\/span><span style=\"color: #B5CEA8\">20<\/span><span style=\"color: #D4D4D4\">|<\/span><span style=\"color: #DCDCAA\">next<\/span><span style=\"color: #D4D4D4\">] <\/span><span style=\"color: #F44747\">-&gt;<\/span><span style=\"color: #D4D4D4\"> [prev|<\/span><span style=\"color: #B5CEA8\">30<\/span><span style=\"color: #D4D4D4\">|<\/span><span style=\"color: #DCDCAA\">next<\/span><span style=\"color: #D4D4D4\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        Scattered memory locations<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-memory-usage-analysis\">Memory Usage Analysis<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Aspect<\/th><th>Array\/List<\/th><th>Doubly Linked List<\/th><\/tr><\/thead><tbody><tr><td><strong>Memory per element<\/strong><\/td><td>4-8 bytes (just data)<\/td><td>12-24 bytes (data + 2 pointers)<\/td><\/tr><tr><td><strong>Memory layout<\/strong><\/td><td>Continuous<\/td><td>Scattered<\/td><\/tr><tr><td><strong>Memory efficiency<\/strong><\/td><td>High<\/td><td>Lower (due to pointer overhead)<\/td><\/tr><tr><td><strong>Cache performance<\/strong><\/td><td>Excellent<\/td><td>Poor<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Example:<\/strong>&nbsp;Storing 1000 integers<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Array:<\/strong>&nbsp;~4KB (4 bytes \u00d7 1000)<\/li>\n\n\n\n<li><strong>Doubly Linked List:<\/strong>&nbsp;~12KB (12 bytes \u00d7 1000)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-pros-and-cons\">Pros and Cons<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-advantages-\"><strong>Advantages<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Bidirectional Traversal<\/strong>\n<ul class=\"wp-block-list\">\n<li>Can move forward and backward easily<\/li>\n\n\n\n<li>No need to restart from the beginning<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Efficient Insertion\/Deletion<\/strong>\n<ul class=\"wp-block-list\">\n<li>O(1) time if you have the node reference<\/li>\n\n\n\n<li>No need to shift elements like in arrays<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Dynamic Size<\/strong>\n<ul class=\"wp-block-list\">\n<li>Can grow and shrink during runtime<\/li>\n\n\n\n<li>No need to declare size beforehand<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>No Memory Waste<\/strong>\n<ul class=\"wp-block-list\">\n<li>Only allocates memory as needed<\/li>\n\n\n\n<li>No unused reserved space<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-disadvantages-\"><strong>Disadvantages<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Higher Memory Usage<\/strong>\n<ul class=\"wp-block-list\">\n<li>Extra memory for storing two pointers per node<\/li>\n\n\n\n<li>Approximately 3x more memory than arrays<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Poor Cache Performance<\/strong>\n<ul class=\"wp-block-list\">\n<li>Nodes scattered in memory<\/li>\n\n\n\n<li>CPU cache misses more frequent<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>No Random Access<\/strong>\n<ul class=\"wp-block-list\">\n<li>Can&#8217;t directly access element at index i<\/li>\n\n\n\n<li>Must traverse from head or tail<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Complex Implementation<\/strong>\n<ul class=\"wp-block-list\">\n<li>More pointers to manage<\/li>\n\n\n\n<li>Higher chance of bugs<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"12-complete-code-walkthrough\">Complete Code Walkthrough<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-node-structure\">Node Structure<\/h3>\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:#D4D4D4;--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:#1E1E1E\"><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):\n        self.val = val      # Data stored in the node\n        self.next = None    # Pointer to next node\n        self.prev = None    # Pointer to previous node (key difference!)\" style=\"color:#D4D4D4;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 dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Node<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">__init__<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">val<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.val = val      <\/span><span style=\"color: #6A9955\"># Data stored in the node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.next = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Pointer to next node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.prev = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Pointer to previous node (key difference!)<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>Explanation:<\/strong>&nbsp;Each node has three parts &#8211; the data and two pointers. This is what makes it &#8220;doubly&#8221; linked!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-basic-operations\">Basic Operations<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-1-insert-at-head-\">1.&nbsp;<strong>Insert at Head<\/strong><\/h3>\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:#D4D4D4;--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:#1E1E1E\"><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_head(self, val):\n    new_node = Node(val)\n    if not self.head:\n        self.head = new_node  # First node becomes head\n    else:\n        new_node.next = self.head    # New node points to current head\n        self.head.prev = new_node    # Current head's prev points to new node\n        self.head = new_node         # Update head to new node\" style=\"color:#D4D4D4;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 dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">insert_at_head<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">val<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    new_node = Node(val)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head = new_node  <\/span><span style=\"color: #6A9955\"># First node becomes head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        new_node.next = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head    <\/span><span style=\"color: #6A9955\"># New node points to current head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head.prev = new_node    <\/span><span style=\"color: #6A9955\"># Current head&#39;s prev points to new node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head = new_node         <\/span><span style=\"color: #6A9955\"># Update head to new node<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong>&nbsp;Like adding a new car to the front of a train &#8211; you need to connect it to the current first car and update what&#8217;s considered the &#8220;front.&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-2-insert-at-end-append-\">2.&nbsp;<strong>Insert at End (Append)<\/strong><\/h3>\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:#D4D4D4;--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:#1E1E1E\"><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, val):\n    new_node = Node(val)\n    if not self.head:\n        self.head = new_node\n    else:\n        current = self.head\n        while current.next:        # Find the last node\n            current = current.next\n        current.next = new_node    # Connect last node to new node\n        new_node.prev = current    # Connect new node back to last node\" style=\"color:#D4D4D4;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 dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">append<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">val<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    new_node = Node(val)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head = new_node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> current.next:        <\/span><span style=\"color: #6A9955\"># Find the last node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            current = current.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current.next = new_node    <\/span><span style=\"color: #6A9955\"># Connect last node to new node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        new_node.prev = current    <\/span><span style=\"color: #6A9955\"># Connect new node back to last node<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong>&nbsp;Like adding a new car to the end of a train &#8211; find the last car and connect the new car to it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"17-3-insert-at-position-\">3.&nbsp;<strong>Insert at Position<\/strong><\/h3>\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:#D4D4D4;--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:#1E1E1E\"><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, val, position):\n    new_node = Node(val)\n    if position == 0:\n        self.insert_at_head(val)\n        return\n    \n    current = self.head\n    count = 0\n    while current and count &lt; position - 1:\n        current = current.next\n        count += 1\n    \n    if current is None:\n        print(&quot;Position out of bounds&quot;)\n        return\n    \n    new_node.next = current.next    # Connect new node to next node\n    new_node.prev = current         # Connect new node to current node\n    if current.next:\n        current.next.prev = new_node  # Update next node's prev pointer\n    current.next = new_node         # Connect current node to new node\" style=\"color:#D4D4D4;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 dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">insert_at<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">val<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">position<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    new_node = Node(val)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> position == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.insert_at_head(val)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    current = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    count = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> current <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> count &lt; position - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current = current.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        count += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> current <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Position out of bounds&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    new_node.next = current.next    <\/span><span style=\"color: #6A9955\"># Connect new node to next node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    new_node.prev = current         <\/span><span style=\"color: #6A9955\"># Connect new node to current node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> current.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current.next.prev = new_node  <\/span><span style=\"color: #6A9955\"># Update next node&#39;s prev pointer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    current.next = new_node         <\/span><span style=\"color: #6A9955\"># Connect current node to new node<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong>&nbsp;Like inserting a new car between two cars in a train &#8211; you need to connect it to both the car before and after.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-4-traversal-operations-\">4.&nbsp;<strong>Traversal Operations<\/strong><\/h3>\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:#D4D4D4;--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:#1E1E1E\"><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_forward(self):\n    current = self.head\n    while current:\n        print(current.val, end=&quot; &quot;)\n        current = current.next    # Move forward\n    print()\n\ndef traverse_backward(self):\n    current = self.head\n    while current.next:        # Go to the last node\n        current = current.next\n    while current:\n        print(current.val, end=&quot; &quot;)\n        current = current.prev  # Move backward\n    print()\" style=\"color:#D4D4D4;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 dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">traverse_forward<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    current = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> current:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(current.val, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&quot; &quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current = current.next    <\/span><span style=\"color: #6A9955\"># Move forward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">traverse_backward<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    current = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> current.next:        <\/span><span style=\"color: #6A9955\"># Go to the last node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current = current.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> current:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(current.val, <\/span><span style=\"color: #9CDCFE\">end<\/span><span style=\"color: #D4D4D4\">=<\/span><span style=\"color: #CE9178\">&quot; &quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        current = current.prev  <\/span><span style=\"color: #6A9955\"># Move backward<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">()<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p><strong>How it works:<\/strong>&nbsp;Forward traversal is like walking through train cars from front to back, while backward traversal is like walking from back to front.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"19-when-to-use-doubly-linked-lists\">When to Use Doubly Linked Lists<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"20-use-when-\"><strong>Use When:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You need&nbsp;<strong>bidirectional traversal<\/strong>&nbsp;(music players, browsers)<\/li>\n\n\n\n<li><strong>Frequent insertions\/deletions<\/strong>&nbsp;in the middle<\/li>\n\n\n\n<li><strong>Dynamic size<\/strong>&nbsp;requirements<\/li>\n\n\n\n<li><strong>Undo\/Redo functionality<\/strong>&nbsp;needed<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"21-dont-use-when-\"><strong>Don&#8217;t Use When:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Memory is limited<\/strong>&nbsp;(mobile apps, embedded systems)<\/li>\n\n\n\n<li>You need&nbsp;<strong>random access<\/strong>&nbsp;to elements<\/li>\n\n\n\n<li><strong>Cache performance<\/strong>&nbsp;is critical<\/li>\n\n\n\n<li><strong>Simple sequential processing<\/strong>&nbsp;is sufficient<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"22-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Array<\/th><th>Singly Linked List<\/th><th>Doubly Linked List<\/th><\/tr><\/thead><tbody><tr><td><strong>Memory per element<\/strong><\/td><td>4-8 bytes<\/td><td>8-16 bytes<\/td><td>12-24 bytes<\/td><\/tr><tr><td><strong>Random access<\/strong><\/td><td>O(1)<\/td><td>O(n)<\/td><td>O(n)<\/td><\/tr><tr><td><strong>Insertion at head<\/strong><\/td><td>O(n)<\/td><td>O(1)<\/td><td>O(1)<\/td><\/tr><tr><td><strong>Insertion at end<\/strong><\/td><td>O(1)<\/td><td>O(n)<\/td><td>O(n)<\/td><\/tr><tr><td><strong>Bidirectional traversal<\/strong><\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u2705<\/td><\/tr><tr><td><strong>Cache performance<\/strong><\/td><td>Excellent<\/td><td>Poor<\/td><td>Poor<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Key Takeaway:<\/strong>&nbsp;Doubly linked lists are perfect when you need the flexibility to move in both directions and don&#8217;t mind the extra memory cost. They&#8217;re like having a two-way street instead of a one-way street &#8211; more versatile but requires more infrastructure!<\/p>\n\n\n\n<p>Choose doubly linked lists when the benefits of bidirectional traversal outweigh the memory overhead for your specific use case.<\/p>\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>What is a Doubly Linked List? A&nbsp;doubly linked list&nbsp;is a type of linked list where each node contains data and&nbsp;two pointers&nbsp;&#8211; one pointing to the next node and another pointing to the previous node. Think of it as a chain where each link is connected to both the link before it and the link after<\/p>\n","protected":false},"author":1,"featured_media":691,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[30],"class_list":{"0":"post-690","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-doubly-linked-list"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/understanding-doubly-linked-lists-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\/690","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=690"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions"}],"predecessor-version":[{"id":693,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions\/693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/691"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}