351. Find the Fractional Node (Every Kth Node)

EasyLinked ListLinked ListTraversalKth Node

Given the head of a singly linked list and an integer k, return the values of every kth node (positions k, 2k, 3k, ...) in the list.

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

Output: Array of values at positions k, 2k, 3k, ...

Examples

Example 1
Input: [1,2,3,4,5,6,7,8,9,10], 3
Output: [3,6,9]
Explanation: Positions 3,6,9.
Example 2
Input: [1,2,3,4,5], 2
Output: [2,4]
Explanation: Every 2nd node.
Example 3
Input: [1,2,3,4,5], 1
Output: [1,2,3,4,5]
Explanation: k=1: every node.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →