{"id":26981,"date":"2017-12-28T21:55:09","date_gmt":"2017-12-28T16:25:09","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26981"},"modified":"2017-12-28T21:55:09","modified_gmt":"2017-12-28T16:25:09","slug":"java-programming-write-function-get-nth-node-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-write-function-get-nth-node-linked-list\/","title":{"rendered":"Java Programming &#8211; Write a function to get Nth node in a Linked List"},"content":{"rendered":"<p>Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position.<br \/>\n<span id=\"more-88\"><\/span><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre>Input:  1->10->30->14,  index = 2\r\nOutput: 30  \r\nThe node at index 2 is 30\r\n<\/pre>\n<div id=\"practice\">\n<p><strong>Algorithm:<\/strong><\/p>\n<pre>1. Initialize count = 0\r\n2. Loop through the link list\r\n     a. if count is equal to the passed index then return current\r\n         node\r\n     b. Increment count\r\n     c. change current to point to next of the current.\r\n<\/pre>\n[ad type=\u201dbanner\u201d]\n<p>Java Programming:<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20find%20n\u2019th%20node%20in%20linked%20list%0A%20%0Aclass%20Node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20Node%20next%3B%0A%20%20%20%20Node(int%20d)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%2F%2Fthe%20head%20of%20list%0A%20%0A%20%20%20%20%2F*%20Takes%20index%20as%20argument%20and%20return%20data%20at%20index*%2F%0A%20%20%20%20public%20int%20GetNth(int%20index)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20current%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20int%20count%20%3D%200%3B%20%2F*%20index%20of%20Node%20we%20are%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%20currently%20looking%20at%20*%2F%0A%20%20%20%20%20%20%20%20while%20(current%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%20if%20(count%20%3D%3D%20index)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20current.data%3B%0A%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%20current%20%3D%20current.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20if%20we%20get%20to%20this%20line%2C%20the%20caller%20was%20asking%0A%20%20%20%20%20%20%20%20for%20a%20non-existent%20element%20so%20we%20assert%20fail%20*%2F%0A%20%20%20%20%20%20%20%20assert(false)%3B%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Given%20a%20reference%20to%20the%20head%20of%20a%20list%20and%20an%20int%2C%0A%20%20%20%20%20%20%20inserts%20a%20new%20Node%20on%20the%20front%20of%20the%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%201.%20alloc%20the%20Node%20and%20put%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*%202.%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*%203.%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*%20Drier%20program%20to%20test%20above%20functions*%2F%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F*%20Start%20with%20empty%20list%20*%2F%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Use%20push()%20to%20construct%20below%20list%0A%20%20%20%20%20%20%20%20%20%20%201-%3E12-%3E1-%3E4-%3E1%20%20*%2F%0A%20%20%20%20%20%20%20%20llist.push(1)%3B%0A%20%20%20%20%20%20%20%20llist.push(4)%3B%0A%20%20%20%20%20%20%20%20llist.push(1)%3B%0A%20%20%20%20%20%20%20%20llist.push(12)%3B%0A%20%20%20%20%20%20%20%20llist.push(1)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20Check%20the%20count%20function%20*%2F%0A%20%20%20%20%20%20%20%20System.out.println(%22Element%20at%20index%203%20is%20%22%2Bllist.GetNth(3))%3B%0A%20%20%20%20%7D%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>Element at index 3 is 4<\/pre>\n<p><strong>Time Complexity:<\/strong> O(n)<\/p>\n[ad type=\u201dbanner\u201d]\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Java Programming &#8211; Write a function to get Nth node in a Linked List &#8211; Linked List &#8211; Write a GetNth() function that takes a linked list and an integer index<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139,79476,79478],"tags":[80286,80499,80490,80492,80489,80493,80495,80498,79571,80488,80491,80288,80494,80497,80285,80496],"class_list":["post-26981","post","type-post","status-publish","format-standard","hentry","category-java","category-linked-list","category-singly-linked-list","tag-algorithm-to-search-an-element-in-singly-linked-list","tag-c-program-to-search-for-an-element-in-the-linked-list-using-recursion","tag-delete-nth-element-in-linked-list","tag-find-middle-element-in-linked-list-java","tag-find-nth-element-in-linked-list-c","tag-find-nth-to-last-element-in-a-linked-list-recursion","tag-insert-a-node-at-a-specific-position-in-a-linked-list-c","tag-linear-search-using-linked-list-in-c","tag-linked-list-search-java","tag-nth-node-from-the-end-of-a-linked-list-java","tag-search-a-node-in-linked-list-in-c","tag-search-an-element-in-doubly-linked-list","tag-search-node-in-linked-list-c","tag-searching-a-linked-list-in-data-structure","tag-searching-in-linked-list-algorithm","tag-searching-in-linked-list-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26981","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=26981"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26981\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}