{"id":2293,"date":"2026-07-20T12:43:14","date_gmt":"2026-07-20T10:43:14","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-reverse-a-linked-list\/"},"modified":"2026-08-26T21:15:56","modified_gmt":"2026-08-26T19:15:56","slug":"how-to-reverse-a-linked-list","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-reverse-a-linked-list\/","title":{"rendered":"How to Reverse A Linked List"},"content":{"rendered":"<p><!-- ktg-updated-banner --><\/p>\n<p><em>Updated August 2026 \u2014 full tutorial restored for this URL.<\/em><\/p>\n<p><strong>Question:<\/strong> How do you reverse a singly linked list? Example: <code>1 \u2192 2 \u2192 3 \u2192 4 \u2192 null<\/code> becomes <code>4 \u2192 3 \u2192 2 \u2192 1 \u2192 null<\/code>.<\/p>\n<p>Companion guide: <a href=\"https:\/\/kindsonthegenius.com\/blog\/everything-about-linked-list-with-python-code-and-hackerrank-leetcode-solutions\/\">Everything About Linked List (Python)<\/a>.<\/p>\n<ol>\n<li><a href=\"#t1\">Node definition<\/a><\/li>\n<li><a href=\"#t2\">Iterative reverse (preferred in interviews)<\/a><\/li>\n<li><a href=\"#t3\">Recursive reverse<\/a><\/li>\n<li><a href=\"#t4\">Python version<\/a><\/li>\n<li><a href=\"#t5\">Complexity<\/a><\/li>\n<\/ol>\n<p><strong id=\"t1\">1. Node definition<\/strong><\/p>\n<pre><code>class ListNode {\n  int val;\n  ListNode next;\n  ListNode(int val) { this.val = val; }\n}\n<\/code><\/pre>\n<p><strong id=\"t2\">2. Iterative reverse (preferred in interviews)<\/strong><\/p>\n<pre><code>ListNode reverseList(ListNode head) {\n  ListNode prev = null;\n  ListNode curr = head;\n  while (curr != null) {\n    ListNode next = curr.next;\n    curr.next = prev;\n    prev = curr;\n    curr = next;\n  }\n  return prev;\n}\n<\/code><\/pre>\n<p>Dry-run on <code>1\u21922\u21923<\/code>: after each step the <code>prev<\/code> chain grows backward until <code>curr<\/code> is null; return <code>prev<\/code>.<\/p>\n<p><strong id=\"t3\">3. Recursive reverse<\/strong><\/p>\n<pre><code>ListNode reverseList(ListNode head) {\n  if (head == null || head.next == null) return head;\n  ListNode newHead = reverseList(head.next);\n  head.next.next = head;\n  head.next = null;\n  return newHead;\n}\n<\/code><\/pre>\n<p><strong id=\"t4\">4. Python version<\/strong><\/p>\n<pre><code>def reverse_list(head):\n    prev, curr = None, head\n    while curr:\n        nxt = curr.next\n        curr.next = prev\n        prev, curr = curr, nxt\n    return prev\n<\/code><\/pre>\n<p><strong id=\"t5\">5. Complexity<\/strong><\/p>\n<ul>\n<li>Time O(n), extra space O(1) iterative \/ O(n) stack recursive<\/li>\n<li>Watch for cycles if the list might be corrupted<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Updated August 2026 \u2014 full tutorial restored for this URL. Question: How do you reverse a singly linked list? Example: 1 \u2192 2 \u2192 3 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[35],"tags":[],"class_list":["post-2293","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"acf":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2293"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions"}],"predecessor-version":[{"id":2457,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2293\/revisions\/2457"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}