劍指offer第二版-22.鏈表中倒數(shù)第k個節(jié)點

本系列導(dǎo)航:劍指offer(第二版)java實現(xiàn)導(dǎo)航帖

面試題22:鏈表中倒數(shù)第k個節(jié)點

題目要求:
求鏈表中倒數(shù)第k個節(jié)點。鏈表的尾節(jié)點定義為倒數(shù)第1個節(jié)點。

解題思路:
如果先求鏈表的長度,計算后再從頭遍歷,雖然時間復(fù)雜度是o(n),但需要兩遍掃描。更好的方式是使用兩個距離為k的指針向右移動,這種方式只需掃描一遍。
chapter3主要考察細節(jié),這道題也不例外。需要注意鏈表是否為空,鏈表長度是否達到k,k是否是個正數(shù)。

package structure;
/**
 * Created by ryder on 2017/6/13.
 */
public class ListNode<T> {
    public T val;
    public ListNode<T> next;
    public ListNode(T val){
        this.val = val;
        this.next = null;
    }
    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append("[");
        for(ListNode cur = this;;cur=cur.next){
            if(cur==null){
                ret.deleteCharAt(ret.lastIndexOf(" "));
                ret.deleteCharAt(ret.lastIndexOf(","));
                break;
            }
            ret.append(cur.val);
            ret.append(", ");
        }
        ret.append("]");
        return ret.toString();
    }
}
package chapter3;
import structure.ListNode;
/**
 * Created by ryder on 2017/7/14.
 * 鏈表中倒數(shù)第k個節(jié)點
 */
public class P134_KthNodeFromEnd {
    public static ListNode<Integer> findKthToTail(ListNode<Integer> head,int k){
        if(head==null||k<=0)
            return null;
        ListNode<Integer> slow=head,fast=head;
        for(int i=0;i<k;i++){
            //i==k-1,第三個測試例通不過
            if(fast.next!=null || i==k-1)
                fast = fast.next;
            else
                return null;
        }
        while(fast!=null){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
    public static void main(String[] args){
        ListNode<Integer> head = new ListNode<>(1);
        head.next= new ListNode<>(2);
        head.next.next = new ListNode<>(3);
        System.out.println(findKthToTail(head,1).val);
        System.out.println(findKthToTail(head,2).val);
        System.out.println(findKthToTail(head,3).val);
        System.out.println(findKthToTail(head,4));
    }
}

運行結(jié)果

3
2
1
null
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容