码迷,mamicode.com
首页 > 其他好文 > 详细

剑指offer【13】- 链表中倒数第k个结点

时间:2019-08-05 20:34:50      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:col   while   bsp   nod   ==   for   node   tno   solution   

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

 

 1 /*
 2 public class ListNode {
 3     int val;
 4     ListNode next = null;
 5 
 6     ListNode(int val) {
 7         this.val = val;
 8     }
 9 }*/
10 public class Solution {
11     public ListNode FindKthToTail(ListNode head,int k) {
12         
13         if(head == null){
14             return head;
15         }
16         int count = 0;
17         ListNode list = head;
18         
19         while(list != null){
20             count++;
21             list = list.next;
22         }
23         if(k > count){
24             return null;
25         }
26         
27         ListNode node = head;
28         for(int i = 0; i < count -k; i++){
29             node = node.next;
30         }
31         return node;
32 
33     }
34 }

 

剑指offer【13】- 链表中倒数第k个结点

标签:col   while   bsp   nod   ==   for   node   tno   solution   

原文地址:https://www.cnblogs.com/linliquan/p/11305243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!