{"id":26982,"date":"2017-12-28T22:01:04","date_gmt":"2017-12-28T16:31:04","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26982"},"modified":"2018-11-01T09:53:16","modified_gmt":"2018-11-01T04:23:16","slug":"python-programming-write-function-get-nth-node-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-write-function-get-nth-node-linked-list\/","title":{"rendered":"Write a function to get Nth node in Linked List"},"content":{"rendered":"<p><span style=\"color: #003366;\"><strong>Write a function to get Nth node in a Linked List Program<\/strong><\/span><\/p>\n<p>Write a<strong> GetNth() function<\/strong> that takes a <a href=\"https:\/\/www.wikitechy.com\/technology\/python-algorithm-write-function-reverse-linked-list\/\" target=\"_blank\" rel=\"noopener\">linked list<\/a> and an <a href=\"https:\/\/www.wikitechy.com\/step-by-step-tutorials\/php\/php-datatype-integer\" target=\"_blank\" rel=\"noopener\">integer<\/a> index and returns the data value stored in the node at that index position.<br \/>\n<span id=\"more-88\"><\/span><\/p>\n<h3 id=\"linked-list\"><span style=\"color: #0000ff;\">Linked List:<\/span><\/h3>\n<p>A linked list is a sequence of data elements, which are connected together through links. Each data element contains a connection to another data element in form of a <a href=\"https:\/\/www.wikitechy.com\/tutorials\/c-programming\/what-is-pointer-in-c\" target=\"_blank\" rel=\"noopener\">pointer<\/a>. <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/programs-for-printing-pyramid-patterns-in-python\" target=\"_blank\" rel=\"noopener\">Python<\/a> does not have linked lists in its standard library.<\/p>\n<h3 id=\"example\"><span style=\"color: #800080;\"><strong>Example:<\/strong><\/span><\/h3>\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<h3 id=\"algorithm\"><span style=\"color: #333300;\"><strong>Algorithm:<\/strong><\/span><\/h3>\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<h3 id=\"python-programming-for-write-a-function-to-get-nth-node-in-a-linked-list\"><span style=\"color: #800000;\">Python Programming for<span style=\"color: #333300;\">\u00a0<span style=\"color: #800000;\"><strong>Write a function to get Nth node in a Linked List<\/strong><\/span><\/span><\/span><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20A%20complete%20working%20Python%20program%20to%20find%20n\u2019th%20node%0A%23%20in%20a%20linked%20list%0A%20%0A%23%20Node%20class%0Aclass%20Node%3A%0A%20%20%20%20%23%20Function%20to%20initialise%20the%20node%20object%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%20%23%20Assign%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%20%23%20Initialize%20next%20as%20null%0A%20%0A%20%0A%23%20Linked%20List%20class%20contains%20a%20Node%20object%0Aclass%20LinkedList%3A%0A%20%0A%20%20%20%20%23%20Function%20to%20initialize%20head%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%0A%20%20%20%20%23%20This%20function%20is%20in%20LinkedList%20class.%20It%20inserts%0A%20%20%20%20%23%20a%20new%20node%20at%20the%20beginning%20of%20Linked%20List.%0A%20%20%20%20def%20push(self%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201%20%26%202%3A%20Allocate%20the%20Node%20%26%0A%20%20%20%20%20%20%20%20%23%20%20%20%20%20Put%20in%20the%20data%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%203.%20Make%20next%20of%20new%20Node%20as%20head%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20self.head%0A%20%0A%20%20%20%20%20%20%20%20%23%204.%20Move%20the%20head%20to%20point%20to%20new%20Node%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%0A%20%20%20%20%23%20Returns%20data%20at%20given%20index%20in%20linked%20list%0A%20%20%20%20def%20getNth(self%2C%20index)%3A%0A%20%20%20%20%20%20%20%20current%20%3D%20self.head%20%23%20Initialise%20temp%0A%20%20%20%20%20%20%20%20count%20%3D%200%20%23%20Index%20of%20current%20node%0A%20%0A%20%20%20%20%20%20%20%20%23%20Loop%20while%20end%20of%20linked%20list%20is%20not%20reached%0A%20%20%20%20%20%20%20%20while%20(current)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(count%20%3D%3D%20index)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20current.data%0A%20%20%20%20%20%20%20%20%20%20%20%20count%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.next%0A%20%0A%20%20%20%20%20%20%20%20%23%20if%20we%20get%20to%20this%20line%2C%20the%20caller%20was%20asking%0A%20%20%20%20%20%20%20%20%23%20for%20a%20non-existent%20element%20so%20we%20assert%20fail%0A%20%20%20%20%20%20%20%20assert(false)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%23%20Code%20execution%20starts%20here%0Aif%20__name__%3D%3D\u2019__main__\u2019%3A%0A%20%0A%20%20%20%20llist%20%3D%20LinkedList()%0A%20%0A%20%20%20%20%23%20Use%20push()%20to%20construct%20below%20list%0A%20%20%20%20%23%201-%3E12-%3E1-%3E4-%3E1%0A%20%20%20%20llist.push(1)%3B%0A%20%20%20%20llist.push(4)%3B%0A%20%20%20%20llist.push(1)%3B%0A%20%20%20%20llist.push(12)%3B%0A%20%20%20%20llist.push(1)%3B%0A%20%0A%20%20%20%20n%20%3D%203%0A%20%20%20%20print%20\u2019Element%20at%20index%203%20is%20%3A\u2019%2C%20llist.getNth(n)\u201d message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h3>\n<pre>Element at index 3 is 4<\/pre>\n<p><span style=\"color: #0000ff;\"><strong>Time Complexity:<\/strong><\/span> <strong>O(n)<\/strong><\/p>\n[ad type=\u201dbanner\u201d]\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python 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 <\/p>\n","protected":false},"author":1,"featured_media":31263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,4148,79478],"tags":[80286,80499,80490,80492,80489,80493,80495,80498,79571,80488,80491,80288,80494,80497,80285,80496],"class_list":["post-26982","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-python","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\/26982","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=26982"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26982\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31263"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}