输入一个链表,输出该链表中倒数第k个结点

来源:互联网 发布:scala 数组 切分 编辑:程序博客网 时间:2024/06/04 01:19
问:输入一个链表,输出该链表中倒数第k个结点/*public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/public class Solution {    public ListNode FindKthToTail(ListNode head,int k) {        if(head==null){            return null;        }else if(k==0){            return null;        }else{            ListNode a=head;            ListNode b=head;            int count=1;            while(count<k && a.next!=null){                a=a.next;                count++;            }            if(count<k){                return null;            }            while(a.next!=null){                a=a.next;                b=b.next;            }            return b;        }    }}

阅读全文
0 0
原创粉丝点击