352. Return Kth Node from Beginning

EasyLinked ListLinked ListTraversal

Given the head of a singly linked list and a 1-based integer k, return the value of the kth node from the beginning. k is always valid (1 <= k <= n).

Input: Head of a singly linked list and integer k (1-based).

Output: Integer value of the kth node.

Examples

Example 1
Input: [1,2,3,4,5], 3
Output: 3
Explanation: 3rd node = value 3.
Example 2
Input: [1,2,3,4,5], 1
Output: 1
Explanation: 1st node = head = 1.
Example 3
Input: [1,2,3,4,5], 5
Output: 5
Explanation: 5th (last) node = 5.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →