Length of Singly Linked List

To find the length of a Singly Linked List.

  • Create a temp pointer and an integer variable len - find length of list.
  • Initialize temp with head and len with 0(zero).
  • Move temp from one node to another until it becomes NULL.
  • Increment len variable in every iteration.
int lengthOfList(){
  struct node *temp;
  int len = 0;
  temp = head;
  while(temp!=NULL){
    len++;
    temp = temp->next;
  }
  return len;
}






Related Programs


  1. Create Singly Linked List with N number of nodes and display.
  2. Perform insertion operation on Singly Linked List.
  3. Perform deletion operation on Singly Linked List.
  4. Reverse of Singly Linked List.
  5. Move last node of Singly Linked List to first.
  6. Swap first and last nodes in a Singly Linked List.
  7. Find the length of Singly Linked List.
  8. Find middle node of Singly Linked List