简介
其实单链表的题都挺复杂的.
code
class Solution
{
public:
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
ListNode *removeNthFromEnd(ListNode *head, int n)
{
int len = getLen(head);
if(n == len){
return head->next;
}
ListNode * q = head;
for(int i=1; i<(len - n); i++){
q = q->next;
}
q->next = q->next->next;
return head;
}
int getLen(ListNode * head) {
ListNode * p = head;
int index = 0;
while(p){
index++;
p = p->next;
}
return index;
}
};