Wednesday, January 15, 2025
Google search engine
HomeData Modelling & AIC++ Program To Check Whether The Length Of Given Linked List Is...

C++ Program To Check Whether The Length Of Given Linked List Is Even Or Odd

Given a linked list, the task is to make a function which checks whether the length of the linked list is even or odd.  
Examples:

Input : 1->2->3->4->NULL
Output : Even
Input : 1->2->3->4->5->NULL
Output : Odd

Method 1: Count the codes linearly 
Traverse the entire Linked List and keep counting the number of nodes. As soon as the loop is finished, we can check if the count is even or odd. You may try it yourself.
Method 2: Stepping 2 nodes at a time 
Approach:

1. Take a pointer and move that pointer two nodes at a time
2. At the end, if the pointer is NULL then length is Even, else Odd.

C++




// C++ program to check length
// of a given linklist
#include <bits/stdc++.h>
using namespace std;
 
// Defining structure
class Node
{
    public:
    int data;
    Node* next;
};
 
// Function to check the length
// of linklist
int LinkedListLength(Node* head)
{
    while (head && head->next)
    {
        head = head->next->next;
    }
    if (!head)
        return 0;
    return 1;
}
     
// Push function
void push(Node** head, int info)
{
    // Allocating node
    Node* node = new Node();
     
    // Info into node
    node->data = info;
     
    // Next of new node to head
    node->next = (*head);
 
    // head points to new node
    (*head) = node;
}
 
// Driver code
int main(void)
{
    Node* head = NULL;
     
    // Adding elements to Linked List
    push(&head, 4);
    push(&head, 5);
    push(&head, 7);
    push(&head, 2);
    push(&head, 9);
    push(&head, 6);
    push(&head, 1);
    push(&head, 2);
    push(&head, 0);
    push(&head, 5);
    push(&head, 5);
    int check = LinkedListLength(head);
     
    // Checking for length of
    // linklist
    if(check == 0)
    {
        cout << "Even";
    }
    else
    {
        cout << "Odd";
    }
    return 0;
}
// This is code is contributed by rathbhupendra


Output:  

Odd

Time Complexity: O(n) 
Auxiliary Space: O(1)

Method: Using Recursion

C++




// C++ program to check length
// of a given linklist
#include <bits/stdc++.h>
using namespace std;
 
// Defining structure
class Node
{
    public:
    int data;
    Node* next;
};
 
//Recursive Function to check the length
// of linklist
int LinkedListLength(Node* head)
{
    if(head == NULL)
    {
        return 0;
    }
    return 1 + LinkedListLength(head->next);
}
 
     
// Push function
void push(Node** head, int info)
{
    // Allocating node
    Node* node = new Node();
     
    // Info into node
    node->data = info;
     
    // Next of new node to head
    node->next = (*head);
 
    // head points to new node
    (*head) = node;
}
 
// Driver code
int main(void)
{
    Node* head = NULL;
     
    // Adding elements to Linked List
    push(&head, 4);
    push(&head, 5);
    push(&head, 7);
    push(&head, 2);
    push(&head, 9);
    push(&head, 6);
    push(&head, 1);
    push(&head, 2);
    push(&head, 0);
    push(&head, 5);
    push(&head, 5);
    int check = LinkedListLength(head);
     
    // Checking for length of
    // linklist
    if(check == 0)
    {
        cout << "Even";
    }
    else
    {
        cout << "Odd";
    }
    return 0;
}
// This is code is contributed by vinay pinjala.


Output

Odd

Time Complexity: O(n) 
Auxiliary Space: O(n)

Please refer complete article on Check whether the length of given linked list is Even or Odd for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
19 Jan, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments