Remove Nth Node From End of List(C++)
程序员文章站
2022-03-10 17:57:19
remove nth node from end of list(c++) 。given a linked list, remove then-th node from the end of lis...
remove nth node from end of list(c++) 。given a linked list, remove then-th node from the end of list and return its head.
/**
* 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(head->next==null)
return null;
listnode *prev=head,*cur=head;
for(int i=0;i
cur=cur->next;
if(cur==null)
return head->next;
while(cur->next!=null)
{
cur=cur->next;
prev=prev->next;
}
prev->next=prev->next->next;
return head;
}
};
上一篇: Comparable和Comparator两个比较器,map集合
下一篇: 批量重偏向与批量释放