{"id":3806,"date":"2021-08-10T15:06:16","date_gmt":"2021-08-10T15:06:16","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3806"},"modified":"2022-11-10T11:54:08","modified_gmt":"2022-11-10T11:54:08","slug":"check-whether-the-length-of-given-linked-list-is-even-or-odd","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/","title":{"rendered":"Check whether the length of given linked list is Even or Odd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png\" alt=\"\" \/><\/p>\n<p>This article helps you to tackle the linked list\u2019s challenge i.e. Check whether the length of a given linked list is Even or Odd also this problem is known as an even odd linked list. Let\u2019s understand the problem statement more clearly.<\/p>\n<h3>Problem Statement of even odd linked list <\/h3>\n<p>In this problem we are given a linked list and we have to find the length of the linked list i.e. count the number of nodes in the linked list, and tell whether the linked list is Even or Odd.<\/p>\n<ul>\n<li>&#8216;Even&#8217; means that the linked list contain even number of nodes (<strong>count%2==0<\/strong>).<\/li>\n<li>&#8216;Odd&#8217; means that the count of the number of nodes in linked list is odd (<strong>count%2==1<\/strong>).<\/li>\n<\/ul>\n<h3>Problem Statement Understanding of even odd linked list <\/h3>\n<p>We will be given a linked list as input and we need to count the total nodes in it and say whether the count is even or odd.<\/p>\n<p>Let the input be the list given below:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Untitled-9-01.png\" alt=\"\" \/><\/p>\n<p>Output of above input will be:-  Odd (since count of the total number of nodes are 5 and 5%2 is equal to 1).<\/p>\n<p>We just need to count the total number of nodes in the list and say whether the count is odd or even. For example, in the above test case, the total number of nodes in the list are 5 and 5 is an odd number so the output will be &#8216;Odd&#8217;. Had the count of nodes been an even number, the output would have been &#8216;Even&#8217;.<\/p>\n<h3>Approach 1 of even odd linked list <\/h3>\n<p>In this approach, we simply count the total number of nodes present in the linked list by iterating the given list and depending on whether the count comes odd or even, we print the output.<\/p>\n<p><strong>Time Complexity of even odd linked list <\/strong> &#8211; O(n) [ where n is the number of nodes present in the list].<\/p>\n<p><strong>Space Complexity of even odd linked list <\/strong> &#8211; O(1).<\/p>\n<p>The above approach is pretty trivial but we need to travel the whole list to say whether the count is odd or even. <\/p>\n<p>Can we do better than this?<br \/>\nWell, we can optimise it a bit more. <\/p>\n<h3>Helpful Observations<\/h3>\n<p>If we keep a pointer that will move two nodes at a time then it is obvious that when we will reach the end of the list if the length is odd, it would be pointing to the last node of the list and if the length is even, it would be pointing to a NULL pointer because it would skip the last node since it is advancing by skipping the nodes in pairs.<\/p>\n<h3>Approach 2 of even odd linked list <\/h3>\n<p>In this approach, we keep a temporary pointer pointing to the head of the linked list and move it by skipping two nodes at a time.<\/p>\n<p>When the pointer comes to the end of the linked list, there are two possibilities:<\/p>\n<ul>\n<li>Either it will point to NULL.<\/li>\n<li>Or it will point to the last node of our list. <\/li>\n<\/ul>\n<p>Since we moved our pointer skipping 2 nodes at a time if it is pointing to NULL, it is guaranteed that the length of the input is even, otherwise, it is odd.<\/p>\n<p><strong>Time complexity of even odd linked list <\/strong> &#8211; O(n), asymptotically, time complexity will remain linear but in this approach, we are travelling only half the total number of nodes i.e. n\/2.<\/p>\n<p><strong>Space Complexity of even odd linked list <\/strong> &#8211; O(1)<\/p>\n<h3>Dry run of even odd linked list <\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Untitled-10-01.png\" alt=\"\" \/><\/p>\n<p>Since our head is pointing to the last node of our list, the length of the list is odd. So our output will be &#8216;Odd&#8217;. <\/p>\n<h3>Code Implementation of even odd linked list <\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3809 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_3809 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_3809 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3809 .wpsm_nav-tabs > li.active > a, #tab_container_3809 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3809 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_3809 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_3809 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3809 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3809 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3809 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3809 .wpsm_nav-tabs > li > a:hover , #tab_container_3809 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_3809 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_3809 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_3809 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_3809 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_3809 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_3809 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_3809 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3809 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_3809 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3809 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_3809 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_3809\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3809\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3809_1\" aria-controls=\"tabs_desc_3809_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3809_2\" aria-controls=\"tabs_desc_3809_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3809_3\" aria-controls=\"tabs_desc_3809_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_3809_4\" aria-controls=\"tabs_desc_3809_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_3809\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_3809_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n \r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/\/ Function to check the length of linklist\r\nint LinkedListLength(struct Node* head)\r\n{\r\n    while (head &amp;&amp; head-&gt;next)\r\n    {\r\n        head = head-&gt;next-&gt;next;\r\n    }\r\n    if (!head)\r\n        return 0;\r\n    return 1;\r\n}\r\nvoid push(struct Node** head, int info)\r\n{\r\n    struct Node* node = (struct Node*) malloc(sizeof(struct Node));\r\n     \r\n    node-&gt;data = info;\r\n    node-&gt;next = (*head);\r\n \r\n    (*head) = node;\r\n}\r\n\r\nint main(void)\r\n{\r\n    struct Node* head = NULL;\r\n    push(&amp;head, 4);\r\n    push(&amp;head, 5);\r\n    push(&amp;head, 7);\r\n    push(&amp;head, 2);\r\n    push(&amp;head, 9);\r\n    push(&amp;head, 6);\r\n    push(&amp;head, 1);\r\n    push(&amp;head, 2);\r\n    push(&amp;head, 0);\r\n    push(&amp;head, 5);\r\n    push(&amp;head, 5);\r\n    int check = LinkedListLength(head);\r\n    if(check == 0)\r\n    {\r\n        printf(&quot;Even&#92;n&quot;);\r\n    }\r\n    else\r\n    {\r\n        printf(&quot;Odd&#92;n&quot;);\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3809_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits stdc++.h=&quot;&quot;&gt;\r\nUsing namespace std;\r\nclass Node\r\n{\r\n    public:\r\n    int data;\r\n    Node* next;\r\n    Node(int x){\r\n        data = x;\r\n   next = NULL;\r\n    }\r\n};\r\n \r\nbool is_length_even(Node* head)\r\n{\r\n    while (head &amp;&amp; head-&gt;next)\r\n    {\r\n        head = head-&gt;next-&gt;next;\r\n    }\r\n    if (head != NULL)\r\n        return false;\r\n    return true;\r\n}\r\n\r\nint main(void)\r\n{\r\n    Node* head = NULL;\r\n    head = new Node(7);\r\n    head-&gt;next = new Node(2);\r\n    head-&gt;next-&gt;next = new Node(4);\r\n    head-&gt;next-&gt;next-&gt;next = new Node(23);\r\n    head-&gt;-&gt;next-&gt;next-&gt;next-&gt;next = new Node(31);\r\n    \r\n    if(is_length_even(head)){\r\n        cout&lt;&lt;&rdquo;Even&rdquo;;\r\n    }else{\r\n        cout&lt;&lt;&rdquo;Odd&rdquo;;\r\n    }\r\n    return 0;\r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3809_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\nclass Node \r\n{\r\n    int data;\r\n    Node next;\r\n    Node()\r\n    {\r\n        \r\n    }\r\n    Node(int data)\r\n    {\r\n        this.data=data;\r\n    }\r\n    boolean is_length_even(Node head)\r\n    {\r\n        while(head!=null &amp;&amp; head.next!=null)\r\n        {\r\n            head=head.next.next;\r\n        }\r\n        if(head!=null)\r\n        {\r\n            return false;\r\n        }\r\n        return true;\r\n    }\r\n}\r\n\r\npublic class EvenOdd \r\n{\r\n    public static void main(String[] args) \r\n    {\r\n        Node check=new Node();\r\n        Node head=null;\r\n        head = new Node(7);\r\n        head.next = new Node(2);\r\n        head.next.next = new Node(4);\r\n        head.next.next.next = new Node(23);\r\n        head.next.next.next.next = new Node(31);    \r\n\r\n        if(check.is_length_even(head))\r\n        {\r\n            System.out.println(&quot;Even&quot;);\r\n        }\r\n        else \r\n        {\r\n            System.out.println(&quot;Odd&quot;);\r\n        }\r\n    }    \r\n}\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_3809_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\t\tself.head = None\r\n\r\n\tdef is_length_even(self):\r\n\t\twhile (self.head and self.head.next):\r\n\t\t\tself.head = self.head.next.next\r\n\t\t\t\r\n\t\tif(self.head != None):\r\n\t\t\treturn False\r\n\t\treturn True\r\n\r\nhead = Node(7)\r\nhead.next = Node(2)\r\nhead.next.next = Node(4)\r\nhead.next.next.next = Node(23)\r\nhead.next.next.next.next = Node(31)\r\n\r\ncheck = head.is_length_even()\r\n\r\nif(check == 0) :\r\n\tprint(&quot;Even&quot;)\r\nelse:\r\n\tprint(&quot;Odd&quot;)\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_3809 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_3809 a\"),jQuery(\"#tab-content_3809\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h4>Output<\/h4>\n<p>Odd<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>To conclude this blog, we discussed the problem statement, and different ways to solve the problems. We have discussed two approaches with efficient dry run. We hope this article will helps you to clear your doubts.<br \/>\nIf you want to practice more questions on linked lists, feel free to solve them at <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<\/p>\n<h2>FAQs related to even odd linked list<\/h2>\n<p><ol><strong><\/p>\n<li>Describe the applications of linked lists?<\/li>\n<p><\/strong><br \/>\nLinked list are used for implementing data structures like stack, queue, graph and many more.<br \/>\n<strong><\/p>\n<li>What is the purpose of the dummy header in a linked list?<\/li>\n<p><\/strong><br \/>\nThe dummy header in a linked list includes the first record of the actual data.<br \/>\n<strong><\/p>\n<li>What is the length of the linked list?<\/li>\n<p><\/strong><br \/>\nThe length of the linked list is the total number of nodes in the linked list.<\/ol><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article helps you to tackle the linked list\u2019s challenge i.e. Check whether the length of a given linked list is Even or Odd also this problem is known as an even odd linked list. Let\u2019s understand the problem statement more clearly. Problem Statement of even odd linked list In this problem we are given [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-3806","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linked List | Check whether the length of given linked list is Even or Odd | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to check whether the length of a given linked list is odd or even. This blog explains how you can find whether the length of a given linked list is odd or even.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linked List | Check whether the length of given linked list is Even or Odd | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to check whether the length of a given linked list is odd or even. This blog explains how you can find whether the length of a given linked list is odd or even.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-10T15:06:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-10T11:54:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png\" \/>\n<meta name=\"author\" content=\"PrepBytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PrepBytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Check whether the length of given linked list is Even or Odd\",\"datePublished\":\"2021-08-10T15:06:16+00:00\",\"dateModified\":\"2022-11-10T11:54:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/\"},\"wordCount\":761,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/\",\"name\":\"Linked List | Check whether the length of given linked list is Even or Odd | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png\",\"datePublished\":\"2021-08-10T15:06:16+00:00\",\"dateModified\":\"2022-11-10T11:54:08+00:00\",\"description\":\"Learn the most efficient way to check whether the length of a given linked list is odd or even. This blog explains how you can find whether the length of a given linked list is odd or even.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Check whether the length of given linked list is Even or Odd\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linked List | Check whether the length of given linked list is Even or Odd | Prepbytes","description":"Learn the most efficient way to check whether the length of a given linked list is odd or even. This blog explains how you can find whether the length of a given linked list is odd or even.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/","og_locale":"en_US","og_type":"article","og_title":"Linked List | Check whether the length of given linked list is Even or Odd | Prepbytes","og_description":"Learn the most efficient way to check whether the length of a given linked list is odd or even. This blog explains how you can find whether the length of a given linked list is odd or even.","og_url":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-10T15:06:16+00:00","article_modified_time":"2022-11-10T11:54:08+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Check whether the length of given linked list is Even or Odd","datePublished":"2021-08-10T15:06:16+00:00","dateModified":"2022-11-10T11:54:08+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/"},"wordCount":761,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/","url":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/","name":"Linked List | Check whether the length of given linked list is Even or Odd | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png","datePublished":"2021-08-10T15:06:16+00:00","dateModified":"2022-11-10T11:54:08+00:00","description":"Learn the most efficient way to check whether the length of a given linked list is odd or even. This blog explains how you can find whether the length of a given linked list is odd or even.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185859766-linked%20list%20even%20or%20odd.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/check-whether-the-length-of-given-linked-list-is-even-or-odd\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Check whether the length of given linked list is Even or Odd"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=3806"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3806\/revisions"}],"predecessor-version":[{"id":10439,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3806\/revisions\/10439"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}