Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Length of Linked List

Easy Acceptance 76.00% Points 20.00

You are given the head of a singly linked list. Your task is to determine the total number of nodes present in the linked list and return its length.

Examples
Example 1

Example 1

Input: head = [1,2,3,4,5]

Output: 5

Explanation: The linked list contains 5 nodes, so its length is 5.

Example 2

Example 2

Input: head = [10]

Output: 1

Explanation: The linked list contains only one node, so its length is 1.

Example 3

Example 3

Input: head = [7,14,21,28]

Output: 4

Explanation: The linked list contains 4 nodes, so its length is 4.

Hints
Hint 1
N/A
Constraints
  • 1 = Number of nodes = 105
  • -10? = Node.data = 10?
Companies
Visa
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions