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

【剑指offer】输出链表倒数第K个元素

时间:2018-11-28 12:07:09      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:ext   node   ota   ++   剑指offer   链表   his   bsp   int   

 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         if(head == null) return null;
13         int length = 0;
14         ListNode tempNode = head;
15         ListNode result;
16         while(tempNode!=null){
17             length++;
18             tempNode = tempNode.next;
19         }
20         //判断K值是否合法
21         if(k > length) return null;
22         tempNode = head;
23         for(int i=0; i<length; i++){
24             if(i>=k){
25                 head = head.next;
26             }
27             tempNode  = tempNode.next;
28         }
29         return head;
30     }
31 }

 

【剑指offer】输出链表倒数第K个元素

标签:ext   node   ota   ++   剑指offer   链表   his   bsp   int   

原文地址:https://www.cnblogs.com/singular/p/10030032.html

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