
Implement an algorithm to find the kth to last element of a singly linked list. Return the value of the element.
Note: This problem is slightly different from the original one in the book.
Example:
Input: 1->2->3->4->5 和 k = 2 Output: 4 Note:
k is always valid.
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-node-from-end-of-list-lcci 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int kthToLast(ListNode head, int k) {
ListNode first = head, second = head;
for(int i=0;i<k;i++){
first=first.next;
}
while(first!=null){
first = first.next;
second = second.next;
}
return second.val;
}
}