{"id":26869,"date":"2017-12-26T20:40:32","date_gmt":"2017-12-26T15:10:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26869"},"modified":"2017-12-26T20:40:32","modified_gmt":"2017-12-26T15:10:32","slug":"java-algorithm-find-length-linked-list-iterative-recursive","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-find-length-linked-list-iterative-recursive\/","title":{"rendered":"Java Algorithm &#8211; Find Length of a Linked List both Iterative and Recursive"},"content":{"rendered":"<p>Write a C function to count number of nodes in a given singly linked list.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-26889\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length.png\" alt=\"C Algorithm - Find Length of a Linked List both Iterative and Recursive\" width=\"878\" height=\"184\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length.png 878w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length-300x63.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Linkedlist_find_length-768x161.png 768w\" sizes=\"(max-width: 878px) 100vw, 878px\" \/><\/p>\n<p>For example, the function should return 5 for linked list 1-&gt;3-&gt;1-&gt;2-&gt;1.<\/p>\n<p><strong>Iterative Solution<\/strong><\/p>\n<pre>1) Initialize count as 0 \r\n2) Initialize a node pointer, current = head.\r\n3) Do following while current is not NULL\r\n     a) current = current -&gt; next\r\n     b) count++;\r\n4) Return count<\/pre>\n<p><strong>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to count number of nodes in a linked list<br\/> <br\/>\/* Linked list Node*\/<br\/>class Node<br\/>{<br\/>    int data;<br\/>    Node next;<br\/>    Node(int d)  { data = d;  next = null; }<br\/>}<br\/> <br\/>\/\/ Linked List class<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of list<br\/> <br\/>    \/* Inserts a new Node at front of the list. *\/<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/* 1 &amp; 2: Allocate the Node &amp;<br\/>                  Put in the data*\/<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/* 3. Make next of new Node as head *\/<br\/>        new_node.next = head;<br\/> <br\/>        \/* 4. Move the head to point to new Node *\/<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    \/* Returns count of nodes in linked list *\/<br\/>    public int getCount()<br\/>    {<br\/>        Node temp = head;<br\/>        int count = 0;<br\/>        while (temp != null)<br\/>        {<br\/>            count++;<br\/>            temp = temp.next;<br\/>        }<br\/>        return count;<br\/>    }<br\/> <br\/>    \/* Drier program to test above functions. Ideally<br\/>       this function should be in a separate user class.<br\/>       It is kept here to keep code compact *\/<br\/>    public static void main(String[] args)<br\/>    {<br\/>        \/* Start with the empty list *\/<br\/>        LinkedList llist = new LinkedList();<br\/>        llist.push(1);<br\/>        llist.push(3);<br\/>        llist.push(1);<br\/>        llist.push(2);<br\/>        llist.push(1);<br\/> <br\/>        System.out.println(&quot;Count of nodes is &quot; +<br\/>                           llist.getCount());<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>count of nodes is 5<\/pre>\n<p><strong>Recursive Solution<\/strong><\/p>\n<pre><strong>int getCount(head)<\/strong>\r\n1) If head is NULL, return 0.\r\n2) Else return 1 + getCount(head-&gt;next)<\/pre>\n<p><strong>Java Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Recursive Java program to count number of nodes in <br\/>\/\/ a linked list<br\/> <br\/>\/* Linked list Node*\/<br\/>class Node<br\/>{<br\/>    int data;<br\/>    Node next;<br\/>    Node(int d)  { data = d;  next = null; }<br\/>}<br\/> <br\/>\/\/ Linked List class<br\/>class LinkedList<br\/>{<br\/>    Node head;  \/\/ head of list<br\/> <br\/>    \/* Inserts a new Node at front of the list. *\/<br\/>    public void push(int new_data)<br\/>    {<br\/>        \/* 1 &amp; 2: Allocate the Node &amp;<br\/>                  Put in the data*\/<br\/>        Node new_node = new Node(new_data);<br\/> <br\/>        \/* 3. Make next of new Node as head *\/<br\/>        new_node.next = head;<br\/> <br\/>        \/* 4. Move the head to point to new Node *\/<br\/>        head = new_node;<br\/>    }<br\/> <br\/>    \/* Returns count of nodes in linked list *\/<br\/>    public int getCountRec(Node node)<br\/>    {<br\/>        \/\/ Base case<br\/>        if (node == null)<br\/>            return 0;<br\/> <br\/>        \/\/ Count is this node plus rest of the list<br\/>        return 1 + getCountRec(node.next);<br\/>    }<br\/> <br\/>    \/* Wrapper over getCountRec() *\/<br\/>    public int getCount()<br\/>    {<br\/>        return getCountRec(head);<br\/>    }<br\/> <br\/>    \/* Drier program to test above functions. Ideally<br\/>       this function should be in a separate user class.<br\/>       It is kept here to keep code compact *\/<br\/>    public static void main(String[] args)<br\/>    {<br\/>        \/* Start with the empty list *\/<br\/>        LinkedList llist = new LinkedList();<br\/>        llist.push(1);<br\/>        llist.push(3);<br\/>        llist.push(1);<br\/>        llist.push(2);<br\/>        llist.push(1);<br\/> <br\/>        System.out.println(&quot;Count of nodes is &quot; +<br\/>                           llist.getCount());<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Output:<\/strong><\/p>\n<pre>count of nodes is 5<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java Algorithm &#8211; Find Length of a Linked List both Iterative and Recursive &#8211; Linked List &#8211; Write a C function to count number of nodes in a given singly<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2139,79476,79478],"tags":[72483,80240,80236,80239,80231,80237,80241,80232,80238,80235,80234,80230,80233],"class_list":["post-26869","post","type-post","status-publish","format-standard","hentry","category-coding","category-java","category-linked-list","category-singly-linked-list","tag-c-linked-list","tag-find-length-of-linked-list-java","tag-geeksforgeeks","tag-get-length-of-linked-list-python","tag-how-to-calculate-the-length-of-linked-list-without-using-counter-in-java","tag-how-to-find-the-length-of-a-linked-list-python","tag-length-of-linked-list-c","tag-length-of-linked-list-python","tag-linked-list-implementation-in-python","tag-linkedlist-java","tag-recursive-linked-list-java","tag-size-of-linked-list-c","tag-what-is-a-class-loader-what-does-it-do"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26869","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=26869"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26869\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}