Given the head of a linked list, remove the n<sup>th</sup> node from the end of the list and return its head.
Follow up: Could you do this in one pass?
Example 1:

image
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Example 3:
Input: head = [1,2], n = 1
Output: [1]
Constraints:
- The number of nodes in the list is
sz. 1 <= sz <= 300 <= Node.val <= 1001 <= n <= sz
哈哈哈哈 自己写出来的,用的还是递归的方式,感觉自己很棒啊!
这道题只是删除某个指定的结点,那么就可以使用递归的方式来计算链表的长度,然后和当前长度进行比较,如果达到了指定的节点那么就能够进行删除动作。不过感觉耗时比较多。。。不知咋回事。、
常规解法感觉没啥亮点,这里就不讨论了。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ListNode;
struct ListNode* RecurseDeepIn(struct ListNode* node, int count, int n, int* depth){
if( node != NULL ) node->next = RecurseDeepIn(node->next, count, ++n, depth);
if( node == NULL) *depth = n;
if( *depth != 0 && (*depth - n) == count-1) return node == NULL ? node : node->next;
return node;
}
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
int *depth;
int depthCount = 0;
depth = &depthCount;
head = RecurseDeepIn(head, n, 0, depth);
return head;
}