题目描述
输入一个链表,输出该链表中倒数第k个结点。
举一个简单的例子,比如链表{1,2,3,4,5},如果要返回倒数第二个节点,也就是k=2,就相当于正数第5-k+1=4个节点,所以我们可以采用两次循环:一次循环得到链表的结点个数;另一次循环则是从链表中找到第n-k+1个节点。虽然是两次循环,但时间复杂度是 O(n)
,需要注意的是,这里仍然需要对链表的边界条件进行判断
package com.gpl.offer.jianzhi; /** * Created by gpl on 2016/8/9. */ public class KthToTail { //链表中的倒数第k个节点 public LinkNode findk(LinkNode head,int k){ if(head == null || k < 0) return null; LinkNode front = head; LinkNode behind = null; for(int i=0;i<k-1;i++){ if(front.next!=null) front = front.next; else return null; } behind = head; while(front.next!=null){ front = front.next; behind = behind.next; } return behind; } public static void main(String[] args){ LinkNode head = new LinkNode(1); LinkNode h1 = new LinkNode(2); LinkNode h2 = new LinkNode(3); LinkNode h3 = new LinkNode(4); LinkNode h4 = new LinkNode(5); LinkNode h5 = new LinkNode(6); head.setNext(h1); h1.setNext(h2); h2.setNext(h3); h3.setNext(h4); h4.setNext(h5); h5.setNext(null); KthToTail ktt = new KthToTail(); LinkNode nodeK = ktt.findk(head,6); System.out.println(nodeK.val); } } class LinkNode{ int val; LinkNode next; public LinkNode(int val){ this.val = val; } public void setNext(LinkNode next) { this.next = next; } }