{"id":3563,"date":"2021-08-04T09:55:27","date_gmt":"2021-08-04T09:55:27","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3563"},"modified":"2022-06-22T07:48:41","modified_gmt":"2022-06-22T07:48:41","slug":"can-we-reverse-a-linked-list-in-less-than-on","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/","title":{"rendered":"Can we reverse a linked list in less than O(n)?"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>Reversing a linked list is a very common operation that we need to perform while solving problems on linked list. So, if this operation is used a lot, let&#8217;s look at how fast it can be performed on linked lists of different types. And we will also find the answer to the question: Can we reverse a linked list in less than O(n) time ?<\/p>\n<\/p>\n<p>To answer this question, let\u2019s discuss this operation on two most commonly used linked lists:<\/p>\n<h4>Singly linked list<\/h4>\n<p>In a singly linked list, we have the node as:<\/p>\n<pre><code class=\"language-c++\">struct Node {\n    int val;\n    Node* next;\n};<\/code><\/pre>\n<p>And the singly linked list would look something like:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Can-we-reverse-a-linked-list-in-less-than-On_1-01.png\" alt=\"\" \/><\/p>\n<p>Any method of reversing such a linked list will require at least one traversal of the linked list, visiting each node at least once. Then we can either use extra space or we can simply modify their pointers and reverse them. This can be implemented both recursively and iteratively in linear time. <\/p>\n<p>So, there seems no way to do this work in less than O(n).<\/p>\n<h4>Doubly linked list<\/h4>\n<p>A node in a doubly linked list has a pointer to both next and previous nodes.<\/p>\n<pre><code class=\"language-c++\">struct Node {\n    int val;\n    Node *next, *prev;\n};<\/code><\/pre>\n<p>And the doubly linked list looks something like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Can-we-reverse-a-linked-list-in-less-than-On_2-01.png\" alt=\"\" \/><\/p>\n<p>In this doubly-linked list, one thing we can observe is that, if we traverse from the tail pointer towards the node pointed by the head pointer, then we would be traversing the linked list in reverse order.<\/p>\n<p>So, for doubly-linked lists with a tail pointer, one thing that we can do so that it seems reversed is to swap head and tail pointers, which will take constant time. Now we have a linked list in which to traverse forward we would have to use the <strong>prev<\/strong> pointer and for backward traversal use the <strong>next<\/strong> pointer. Although the linked list would be reversed, it would create a lot of confusion while performing operations on such a linked list (as we need to treat <strong>prev<\/strong> as <strong>next<\/strong> and <strong>next<\/strong> as <strong>prev<\/strong>).<\/p>\n<p>This is the only way where we can reverse a linked list in less than O(n) time.<\/p>\n<p>In the above article, we discussed if we can reverse a linked list in less than O(n) time. Thinking about problems like these is good for strengthening your grip over LinkedList. I would highly recommend you to practice some problems on a linked list from PrepBytes <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Reversing a linked list is a very common operation that we need to perform while solving problems on linked list. So, if this operation is used a lot, let&#8217;s look at how fast it can be performed on linked lists of different types. And we will also find the answer to the question: Can [&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-3563","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>Can we reverse a linked list in less than O(n)?<\/title>\n<meta name=\"description\" content=\"Here we will discuss if we can reverse a linked list in less than O(n) time or not. Thinking about problems like these is good for strengthening your grip over LinkedList.\" \/>\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\/can-we-reverse-a-linked-list-in-less-than-on\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Can we reverse a linked list in less than O(n)?\" \/>\n<meta property=\"og:description\" content=\"Here we will discuss if we can reverse a linked list in less than O(n) time or not. Thinking about problems like these is good for strengthening your grip over LinkedList.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/\" \/>\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-08-04T09:55:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-06-22T07:48:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Can we reverse a linked list in less than O(n)?\",\"datePublished\":\"2021-08-04T09:55:27+00:00\",\"dateModified\":\"2022-06-22T07:48:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/\"},\"wordCount\":406,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/\",\"name\":\"Can we reverse a linked list in less than O(n)?\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png\",\"datePublished\":\"2021-08-04T09:55:27+00:00\",\"dateModified\":\"2022-06-22T07:48:41+00:00\",\"description\":\"Here we will discuss if we can reverse a linked list in less than O(n) time or not. Thinking about problems like these is good for strengthening your grip over LinkedList.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#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\":\"Can we reverse a linked list in less than O(n)?\"}]},{\"@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":"Can we reverse a linked list in less than O(n)?","description":"Here we will discuss if we can reverse a linked list in less than O(n) time or not. Thinking about problems like these is good for strengthening your grip over LinkedList.","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\/can-we-reverse-a-linked-list-in-less-than-on\/","og_locale":"en_US","og_type":"article","og_title":"Can we reverse a linked list in less than O(n)?","og_description":"Here we will discuss if we can reverse a linked list in less than O(n) time or not. Thinking about problems like these is good for strengthening your grip over LinkedList.","og_url":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-04T09:55:27+00:00","article_modified_time":"2022-06-22T07:48:41+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Can we reverse a linked list in less than O(n)?","datePublished":"2021-08-04T09:55:27+00:00","dateModified":"2022-06-22T07:48:41+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/"},"wordCount":406,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/","url":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/","name":"Can we reverse a linked list in less than O(n)?","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png","datePublished":"2021-08-04T09:55:27+00:00","dateModified":"2022-06-22T07:48:41+00:00","description":"Here we will discuss if we can reverse a linked list in less than O(n) time or not. Thinking about problems like these is good for strengthening your grip over LinkedList.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185692108-Can%20we%20reverse%20a%20linked%20list-08.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/can-we-reverse-a-linked-list-in-less-than-on\/#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":"Can we reverse a linked list in less than O(n)?"}]},{"@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\/3563","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=3563"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3563\/revisions"}],"predecessor-version":[{"id":8013,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3563\/revisions\/8013"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}