/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode RemoveNthFromEnd(ListNode head, int n) {
        
         if(head == null){
            return null;
        }
        
        if(n == 1 && head.next == null){
            return null;
        }
        
       var len = 0;
       var h = head;
       while(head != null){
           head = head.next;
           len ++;
       }
       
       if(len - n < 0){
           return null;
       }
       var nth = len - n + 1;
       
       var tmp = h;
       if(nth < len){
	   	var c = 1;
            while(c < nth){
               h = h.next;
               c ++;
            }
			
            h.val = h.next.val;
            h.next = h.next.next;
       }else{
	   		while(h.next.next != null) {
				h = h.next;
			}
           h.next = null;
       }
       
       return tmp;
    }
}版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Remove Nth Node From End of List
原文:http://blog.csdn.net/lan_liang/article/details/48576041