{"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-&gt;10-&gt;30-&gt;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=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># A complete working Python program to find n&#039;th node<br\/># in a linked list<br\/> <br\/># Node class<br\/>class Node:<br\/>    # Function to initialise the node object<br\/>    def __init__(self, data):<br\/>        self.data = data # Assign data<br\/>        self.next = None # Initialize next as null<br\/> <br\/> <br\/># Linked List class contains a Node object<br\/>class LinkedList:<br\/> <br\/>    # Function to initialize head<br\/>    def __init__(self):<br\/>        self.head = None<br\/> <br\/> <br\/>    # This function is in LinkedList class. It inserts<br\/>    # a new node at the beginning of Linked List.<br\/>    def push(self, new_data):<br\/> <br\/>        # 1 &amp; 2: Allocate the Node &amp;<br\/>        #     Put in the data<br\/>        new_node = Node(new_data)<br\/> <br\/>        # 3. Make next of new Node as head<br\/>        new_node.next = self.head<br\/> <br\/>        # 4. Move the head to point to new Node<br\/>        self.head = new_node<br\/> <br\/>    # Returns data at given index in linked list<br\/>    def getNth(self, index):<br\/>        current = self.head # Initialise temp<br\/>        count = 0 # Index of current node<br\/> <br\/>        # Loop while end of linked list is not reached<br\/>        while (current):<br\/>            if (count == index):<br\/>                return current.data<br\/>            count += 1<br\/>            current = current.next<br\/> <br\/>        # if we get to this line, the caller was asking<br\/>        # for a non-existent element so we assert fail<br\/>        assert(false)<br\/>        return 0;<br\/> <br\/># Code execution starts here<br\/>if __name__==&#039;__main__&#039;:<br\/> <br\/>    llist = LinkedList()<br\/> <br\/>    # Use push() to construct below list<br\/>    # 1-&gt;12-&gt;1-&gt;4-&gt;1<br\/>    llist.push(1);<br\/>    llist.push(4);<br\/>    llist.push(1);<br\/>    llist.push(12);<br\/>    llist.push(1);<br\/> <br\/>    n = 3<br\/>    print &#039;Element at index 3 is :&#039;, llist.getNth(n)<\/code><\/pre> <\/div>\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=&#8221;banner&#8221;]\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}]}}