Remove Nth Node From End of List

很简单的一道题,隐含条件要问清楚, n> len的时候就删除首个

public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head==null) return null;
        ListNode walk = head;
        ListNode run =head;
        
        for(int i=0; i<n; i++){
            run = run.next;
        }
        if(run==null) return head.next; // 这个隐含条件要问清楚
        
        while(run.next!=null){
            run = run.next;
            walk=walk.next;
        }
        walk.next = walk.next.next;
        return head;
        
    }
}

 

posted @ 2015-04-06 13:03  世界到处都是小星星  阅读(102)  评论(0编辑  收藏  举报