(19)-(Remove Nth Node From End of List)-(删除倒数第K个元素,我实在是不想写了,有点烦了)

(19)-(Remove Nth Node From End of List)-(删除倒数第K个元素,我实在是不想写了,有点烦了)

public class Solution 
{  
    public ListNode removeNthFromEnd(ListNode head, int n) 
    {  
        // Note: The Solution object is instantiated only once and is reused by each test case.  
        if(head == null)  
            return null;  
        
        ListNode fast_temp = head; 
        ListNode slow_temp = head;  
         
        for(int i = 0; i < n; i++)  
        {  
            fast_temp = fast_temp.next;  
        }  
        if(fast_temp == null)  
        {  
            head = head.next;  
            slow_temp = null;  
            return head;  
        }  
        while(fast_temp.next != null)  
        {  
            slow_temp = slow_temp.next;  
            fast_temp = fast_temp.next;  
        }  
        ListNode curr_tmp = slow_temp.next.next;  
        slow_temp.next = curr_tmp;  
        return head;  
           
    }  
}

 

posted @ 2015-07-29 22:07  爱吃萝卜干  阅读(86)  评论(0编辑  收藏  举报