{"id":27031,"date":"2018-01-02T20:58:58","date_gmt":"2018-01-02T15:28:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27031"},"modified":"2018-01-02T20:58:58","modified_gmt":"2018-01-02T15:28:58","slug":"java-algorithm-find-nth-node-end-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-find-nth-node-end-linked-list\/","title":{"rendered":"Java Algorithm &#8211; Find n\u2019th node from the end of a Linked List"},"content":{"rendered":"<p>Given a Linked List and a number n, write a function that returns the value at the n\u2019th node from end of the Linked List.<\/p>\n<p><strong>Method 1 (Use length of linked list)<\/strong><br \/>\n1) Calculate the length of Linked List. Let the length be len.<br \/>\n2) Print the (len \u2013 n + 1)th node from the begining of the Linked List.<\/p>\n<p><strong>Java Programming:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Simple%20Java%20program%20to%20find%20n\u2019th%20node%20from%20end%20of%20linked%20list%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%2F%2F%20head%20of%20the%20list%0A%20%0A%20%20%20%20%2F*%20Linked%20List%20node%20*%2F%0A%20%20%20%20class%20Node%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%20%20%20%20%20%20%20Node(int%20d)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20get%20the%20nth%20node%20from%20the%20last%20of%20a%0A%20%20%20%20%20%20%20linked%20list%20*%2F%0A%20%20%20%20void%20printNthFromLast(int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20len%20%3D%200%3B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%201)%20count%20the%20number%20of%20nodes%20in%20Linked%20List%0A%20%20%20%20%20%20%20%20while%20(temp%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20len%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20check%20if%20value%20of%20n%20is%20not%20more%20than%20length%20of%0A%20%20%20%20%20%20%20%20%2F%2F%20the%20linked%20list%0A%20%20%20%20%20%20%20%20if%20(len%20%3C%20n)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20temp%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%202)%20get%20the%20(n-len%2B1)th%20node%20from%20the%20begining%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20len-n%2B1%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(temp.data)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Inserts%20a%20new%20Node%20at%20front%20of%20the%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Put%20in%20the%20data*%2F%0A%20%20%20%20%20%20%20%20Node%20new_node%20%3D%20new%20Node(new_data)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20Node%20as%20head%20*%2F%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%20*%2F%0A%20%20%20%20%20%20%20%20head%20%3D%20new_node%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*Drier%20program%20to%20test%20above%20methods%20*%2F%0A%20%20%20%20public%20static%20void%20main(String%20%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20llist.push(20)%3B%0A%20%20%20%20%20%20%20%20llist.push(4)%3B%0A%20%20%20%20%20%20%20%20llist.push(15)%3B%0A%20%20%20%20%20%20%20%20llist.push(35)%3B%0A%20%0A%20%20%20%20%20%20%20%20llist.printNthFromLast(4)%3B%0A%20%20%20%20%7D%0A%7D%2F%2F%20This%20code%20is%20contributed%20by%20Rajat%20Mishra\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>35<\/pre>\n[ad type=\u201dbanner\u201d]\n<p>Following is a recursive C code for the same method. Thanks to Anuj Bansal for providing following code.<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201dvoid%20printNthFromLast(struct%20node*%20head%2C%20int%20n)%20%0A%7B%0A%20%20%20%20static%20int%20i%20%3D%200%3B%0A%20%20%20%20if%20(head%20%3D%3D%20NULL)%0A%20%20%20%20%20%20%20return%3B%0A%20%20%20%20printNthFromLast(head-%3Enext%2C%20n)%3B%0A%20%20%20%20if%20(%2B%2Bi%20%3D%3D%20n)%0A%20%20%20%20%20%20%20printf(%22%25d%22%2C%20head-%3Edata)%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Time Complexity:<\/strong> O(n) where n is the length of linked list.<br \/>\n<strong>Method 2 (Use two pointers) <\/strong><br \/>\nMaintain two pointers \u2013 reference pointer and main pointer. Initialize both reference and main pointers to head. First move reference pointer to n nodes from head. Now move both pointers one by one until reference pointer reaches end. Now main pointer will point to nth node from the end. Return main pointer.<\/p>\n<p><strong>Java Programming:<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20find%20n\u2019th%20node%20from%20end%20using%20slow%20and%0A%2F%2F%20fast%20pointers%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%2F%2F%20head%20of%20the%20list%0A%20%0A%20%20%20%20%2F*%20Linked%20List%20node%20*%2F%0A%20%20%20%20class%20Node%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%20%20%20%20%20%20%20Node(int%20d)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20get%20the%20nth%20node%20from%20end%20of%20list%20*%2F%0A%20%20%20%20void%20printNthFromLast(int%20n)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20main_ptr%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20Node%20ref_ptr%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20int%20count%20%3D%200%3B%0A%20%20%20%20%20%20%20%20if%20(head%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(count%20%3C%20n)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(ref_ptr%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(n%2B%22%20is%20greater%20than%20the%20no%20%22%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22%20of%20nodes%20in%20the%20list%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ref_ptr%20%3D%20ref_ptr.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20count%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20while%20(ref_ptr%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20main_ptr%20%3D%20main_ptr.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ref_ptr%20%3D%20ref_ptr.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Node%20no.%20%22%2Bn%2B%22%20from%20last%20is%20%22%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20main_ptr.data)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Inserts%20a%20new%20Node%20at%20front%20of%20the%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Put%20in%20the%20data*%2F%0A%20%20%20%20%20%20%20%20Node%20new_node%20%3D%20new%20Node(new_data)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20Node%20as%20head%20*%2F%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%20*%2F%0A%20%20%20%20%20%20%20%20head%20%3D%20new_node%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*Drier%20program%20to%20test%20above%20methods%20*%2F%0A%20%20%20%20public%20static%20void%20main(String%20%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20llist.push(20)%3B%0A%20%20%20%20%20%20%20%20llist.push(4)%3B%0A%20%20%20%20%20%20%20%20llist.push(15)%3B%0A%20%20%20%20%20%20%20%20llist.push(35)%3B%0A%20%0A%20%20%20%20%20%20%20%20llist.printNthFromLast(4)%3B%0A%20%20%20%20%7D%0A%7D%20%2F%2F%20This%20code%20is%20contributed%20by%20Rajat%20Mishra\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Node no. 4 from last is 35<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n) where n is the length of linked list<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Java Algorithm &#8211; Find n\u2019th node from the end of a Linked List &#8211; Linked List &#8211; Given a Linked List and a number n, write a function that returns the value<\/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":[80490,80665,80661,80492,80659,80489,80493,80666,80660,80491],"class_list":["post-27031","post","type-post","status-publish","format-standard","hentry","category-linked-list","category-singly-linked-list","tag-delete-nth-element-in-linked-list","tag-delete-nth-element-in-linked-list-java","tag-delete-nth-node-from-end-of-linked-list","tag-find-middle-element-in-linked-list-java","tag-find-nth-element-in-linked-list","tag-find-nth-element-in-linked-list-c","tag-find-nth-to-last-element-in-a-linked-list-recursion","tag-linked-list-in-cc-delete-a-node-at-nth-position","tag-remove-nth-node-from-end-of-list-c","tag-search-a-node-in-linked-list-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27031","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=27031"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27031\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}