给定一个头节点为 head 的链表用于记录一系列核心肌群训练项目编号,请查找并返回倒数第 cnt 个训练项目编号。
示例 1:
输入:head = [2,4,7,8], cnt = 1
输出:8
提示:
1 <= head.length <= 100
0 <= head[i] <= 100
1 <= cnt <= head.length
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode TrainingPlan(ListNode head, int cnt) {
ListNode cur=head,left=head;
for(int i=0;i<cnt;i++)
cur=cur.next;
while(cur!=null){
cur=cur.next;
left=left.next;
}
return left;
}
}
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。