{"id":27537,"date":"2018-02-06T20:56:26","date_gmt":"2018-02-06T15:26:26","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27537"},"modified":"2018-02-06T20:56:26","modified_gmt":"2018-02-06T15:26:26","slug":"c-algorithm-write-recursive-function-print-reverse-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-write-recursive-function-print-reverse-linked-list\/","title":{"rendered":"C Algorithm &#8211; Write a recursive function to print reverse of a Linked List"},"content":{"rendered":"<p>Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.<\/p>\n<p>Note that the question is only about printing the reverse. To reverse the list itself see this<br \/>\n<strong><br \/>\nDifficulty Level:<\/strong> Rookie<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27544\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/rg.png\" alt=\"C Algorithm - Write a recursive function to print reverse of a Linked List\" width=\"363\" height=\"338\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/rg.png 363w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/rg-300x279.png 300w\" sizes=\"(max-width: 363px) 100vw, 363px\" \/><\/p>\n<p><strong>Algorithm<\/strong><\/p>\n<pre>printReverse(head)\r\n  1. call print reverse for hed->next\r\n  2. print head->data<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20to%20print%20reverse%20of%20a%20linked%20list%0A%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%20%20%0A%2F*%20Link%20list%20node%20*%2F%0Astruct%20node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20struct%20node*%20next%3B%0A%7D%3B%0A%20%20%0A%2F*%20Function%20to%20reverse%20the%20linked%20list%20*%2F%0Avoid%20printReverse(struct%20node*%20head)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20case%20%20%0A%20%20%20%20if%20(head%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%2F%2F%20print%20the%20list%20after%20head%20node%0A%20%20%20%20printReverse(head-%3Enext)%3B%0A%20%0A%20%20%20%20%2F%2F%20After%20everything%20else%20is%20printed%2C%20print%20head%0A%20%20%20%20printf(%22%25d%20%20%22%2C%20head-%3Edata)%3B%0A%7D%0A%20%20%0A%2F*UTILITY%20FUNCTIONS*%2F%0A%2F*%20Push%20a%20node%20to%20linked%20list.%20Note%20that%20this%20function%0A%20%20changes%20the%20head%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20char%20new_data)%0A%7B%0A%20%20%20%20%2F*%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%20%0A%20%20%20%20%2F*%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%20%0A%20%20%20%20%2F*%20link%20the%20old%20list%20off%20the%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%20%20%20%0A%20%20%0A%20%20%20%20%2F*%20move%20the%20head%20to%20pochar%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D%20%0A%20%20%0A%2F*%20Drier%20program%20to%20test%20above%20function*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Let%20us%20create%20linked%20list%201-%3E2-%3E3-%3E4%0A%20%20%20%20struct%20node*%20head%20%3D%20NULL%3B%20%20%20%20%0A%20%20%20%20push(%26head%2C%204)%3B%0A%20%20%20%20push(%26head%2C%203)%3B%0A%20%20%20%20push(%26head%2C%202)%3B%0A%20%20%20%20push(%26head%2C%201)%3B%0A%20%20%20%0A%20%20%20%20printReverse(head)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output<\/strong>:<\/p>\n<pre>4 3 2 1<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C Algorithm &#8211; Write a recursive function to print reverse of a Linked List &#8211;  Linked List &#8211; Given a linked list, print reverse of it using   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81713,81717,81714,80300,81342,81716,81715,81344],"class_list":["post-27537","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-singly-linked-list","tag-algorithm-to-reverse-a-singly-linked-list-in-data-structure","tag-print-linked-list-in-reverse-order-java","tag-print-singly-linked-list-in-reverse-order-without-recursion","tag-reverse-a-linked-list-recursively-c","tag-reverse-a-linked-list-using-recursion","tag-reverse-linked-list-in-c","tag-reverse-linked-list-recursive-python","tag-reversing-a-linked-list-in-c-with-explanation"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27537"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27537\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}