标签:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *removeNthFromEnd(ListNode *head, int n) { // if(n==0) return head; int count = 0;//总的链表长度 ListNode *Head = (ListNode *)malloc(sizeof(ListNode)); Head->next = head;//为链表加一个头结点 ListNode *tail = head; while(tail) { count++; tail = tail->next; } //if(count<n) return NULL; int num = count-n; tail = Head; while(num--){//找到要删除节点的前一个节点的位置 tail = tail->next; } tail->next = tail->next->next;//删除节点 return Head->next; } };
Remove Nth Node From End of List
标签:
原文地址:http://www.cnblogs.com/llei1573/p/4339618.html