Value at the Kth Node of a Linked List
Updated
Problem statement
Return the value stored in the k-th node of the linked list that starts at head, counting the head as node 1. If the list has fewer than k nodes, return -1.
Function
kthElement(head: ListNode, k: int) → intExamples
Example 1
head = [8,3,11,6]k = 3return = 11Counting from the head, node 1 holds 8, node 2 holds 3 and node 3 holds 11.
Example 2
head = [8,3,11,6]k = 5return = -1The list has only 4 nodes, so there is no 5th node and the answer is -1.
Example 3
head = [0,9]k = 1return = 0Counting starts at 1, so the first node is the answer. Its value is 0, a real value rather than a missing node.
Constraints
- The list has between
0and1000nodes. 0 <= Node.val <= 10^91 <= k <= 2000
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Detect a Loop in a Linked ListEPAM · ONSITE INTERVIEWEPAM interview questions
- Delete a Doubly Linked List Node at a PositionQualys · ONSITE INTERVIEWQualys interview questions
- Insert After a Linked-List NodeAMD · ONSITE INTERVIEWAMD interview questions
- Reverse a Singly Linked ListGoogle · ONSITE INTERVIEWGoogle interview questions
- Process Singly Linked List OperationsSEDEMAC · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsZS · ONSITE INTERVIEWZS interview questions
- Middle Node Of A Linked ListNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Build and Check a Palindrome Linked ListOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Reverse a Singly Linked ListGEP · ONSITE INTERVIEW
- Merge Two Sorted Linked ListsApple · ONSITE INTERVIEWApple interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences