{"id":151,"date":"2018-02-25T05:43:59","date_gmt":"2018-02-25T05:43:59","guid":{"rendered":"http:\/\/www.datastructure.org\/?p=151"},"modified":"2025-04-21T13:49:11","modified_gmt":"2025-04-21T13:49:11","slug":"reverse_a_singly_linked_list","status":"publish","type":"post","link":"https:\/\/www.bhargaviurs.com\/index.php\/2018\/02\/25\/reverse_a_singly_linked_list\/","title":{"rendered":"Reverse a Singly Linked List"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n<p>The idea is to always keep a reference to the previous-node and next-node. We will always be operating in the context of the current node.<\/p>\n<p>In every iteration, the steps will be:<br \/>1. Get reference to next node.<br \/>2. Detach the link between current and next node.<br \/>3. Create a link between current and previous node.<br \/>4. Update Previous node &#8211; The new previous node will be the present current node.<br \/>5. Update reference to current node &#8211; The new current node will be the present next node.<\/p>\n<p>All of the above performed in that order. The iteration ends when current node becomes null.<\/p>\n<p>We use 3 references:<\/p>\n<ol>\n<li>prev &#8211; refers to the previous node.<\/li>\n<li>curr &#8211; refers to the current node.<\/li>\n<li>next &#8211; refers to the next node.<\/li>\n<\/ol>\n<p>Here is an example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-152\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.40.35-PM.png\" alt=\"\" width=\"3340\" height=\"1864\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.40.35-PM.png 3340w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.40.35-PM-300x167.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.40.35-PM-1024x571.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.40.35-PM-768x429.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.40.35-PM-1536x857.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.40.35-PM-2048x1143.png 2048w\" sizes=\"auto, (max-width: 3340px) 100vw, 3340px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-153\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.30-PM.png\" alt=\"\" width=\"3340\" height=\"1868\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.30-PM.png 3340w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.30-PM-300x168.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.30-PM-1024x573.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.30-PM-768x430.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.30-PM-1536x859.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.30-PM-2048x1145.png 2048w\" sizes=\"auto, (max-width: 3340px) 100vw, 3340px\" \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-154\" style=\"background-color: #ffffff; font-size: 14px;\" src=\"http:\/\/www.datastructure.org\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.45-PM.png\" alt=\"\" width=\"3322\" height=\"1860\" srcset=\"https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.45-PM.png 3322w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.45-PM-300x168.png 300w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.45-PM-1024x573.png 1024w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.45-PM-768x430.png 768w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.45-PM-1536x860.png 1536w, https:\/\/www.bhargaviurs.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-24-at-11.42.45-PM-2048x1147.png 2048w\" sizes=\"auto, (max-width: 3322px) 100vw, 3322px\" \/><\/p>\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">prev = None\ncurr = start\nwhile curr != None: \u00a0 \u00a0 \u00a0 \u00a0\n    next = curr.link \u00a0 \u00a0 \u00a0 \u00a0\n    curr.link = prev \u00a0 \u00a0 \u00a0 \u00a0\n    prev = curr \u00a0 \u00a0 \u00a0 \u00a0\n    curr = next\n    start = prev<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The idea is to always keep a reference to the previous-node and next-node. We will always be operating in the context of the current node. In every iteration, the steps will be:1. Get reference to next node.2. Detach the link between current and next node.3. Create a link between current and previous node.4. Update Previous &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-linked-list","entry"],"_links":{"self":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":1,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/posts\/151\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhargaviurs.com\/index.php\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}