{"id":5749,"date":"2026-02-10T13:51:29","date_gmt":"2026-02-10T13:51:29","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=5749"},"modified":"2026-02-10T15:21:34","modified_gmt":"2026-02-10T15:21:34","slug":"reverse-a-linked-list","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/reverse-a-linked-list\/","title":{"rendered":"Reverse a Linked List"},"content":{"rendered":"<p><strong class=\"ctaHeader3\">Reverse a Linked List<\/strong><br \/>\n<strong>Input<\/strong><\/p>\n<pre>\r\n1 -> 2 -> 3 -> 4 -> 5 -> NULL \r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\n5 -> 4 -> 3 -> 2 -> 1 -> NULL \r\n<\/pre>\n<p><strong>Idea1<\/strong>: Using bruteforce copy the list in to Array and traverse the array backward and create new node<br \/>\n<strong>Idea2<\/strong>: <\/p>\n<ol>\n<li>Use 3 Variables. Prev, Curr, Temp and traverse through list items<\/li>\n<li>Start with Curr in Head and Prev as null<\/li>\n<li>Move forward by making Temp to Curr.next, Curr.next to Prev<\/li>\n<li>Prev should be set to Curr and Curr should be set to Temp<\/li>\n<\/ol>\n<p><em>Refer Adv DSA 3 Note 5 Page 9<\/em><\/p>\n<blockquote><p>\n  Previous -> Current -> Temp\n<\/p><\/blockquote>\n<p><strong>Node.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org;\r\nclass Node{\r\n\tString content;\r\n\tNode next;\r\n\tNode head;\r\n    public Node(String content){\r\n\t this.content = content;\r\n\t}\t\r\n}\r\n<\/pre>\n<p><strong>SimpleLinkedList.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org;\r\n\r\npublic class SimpleLinkedList {\r\n    Node head;\r\n\r\n    public void printLinkedList() {\r\n        Node current = head;\r\n        while (current != null) {\r\n            System.out.println(current.content);\r\n            current = current.next;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>ReverseLinkedList.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ReverseLinkedList {\r\n    public static void main(String&#x5B;] args) {\r\n        SimpleLinkedList simpleLinkedList = new SimpleLinkedList();\r\n\r\n        simpleLinkedList.insertNodeAtEnd(&quot;Anbu&quot;);\r\n        simpleLinkedList.insertNodeAtEnd(&quot;Arul&quot;);\r\n        simpleLinkedList.insertNodeAtEnd(&quot;Arasu&quot;);\r\n        simpleLinkedList.insertNodeAtEnd(&quot;Bharath&quot;);\r\n        simpleLinkedList.insertNodeAtEnd(&quot;Catherine&quot;);\r\n        simpleLinkedList.insertNodeAtEnd(&quot;Davidson&quot;);\r\n\r\n        simpleLinkedList.printLinkedList();\r\n\r\n        reverseLinkedList(simpleLinkedList);\r\n    }\r\n\r\n    public static void reverseLinkedList(SimpleLinkedList simpleLinkedList){\r\n        Node temp = simpleLinkedList.head;\r\n        Node curr = temp;\r\n        Node prev = null;\r\n\r\n\r\n        while(temp != null){\r\n            temp = curr.next;\r\n            curr.next = prev;\r\n            prev = curr;\r\n            curr = temp;\r\n        }\r\n\r\n        simpleLinkedList.head = prev;\r\n\r\n        System.out.println(&quot;-----------------------------------&quot;);\r\n        simpleLinkedList.printLinkedList();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nAnbu\r\nArul\r\nArasu\r\nBharath\r\nCatherine\r\nDavidson\r\n-----------------------------------\r\nDavidson\r\nCatherine\r\nBharath\r\nArasu\r\nArul\r\nAnbu\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Reverse a Linked List Input 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output 5 -> 4 -> 3 -> 2 -> 1 -> NULL Idea1: Using bruteforce copy the list in to Array and traverse the array backward and create new node Idea2: Use 3 Variables. Prev, Curr, Temp and traverse&hellip; <a href=\"https:\/\/codethataint.com\/blog\/reverse-a-linked-list\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[229],"tags":[],"class_list":["post-5749","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=5749"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5749\/revisions"}],"predecessor-version":[{"id":5757,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/5749\/revisions\/5757"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=5749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=5749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=5749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}