设为首页 加入收藏

TOP

Remove Nth Node From End of List(C++)
2018-05-24 08:56:44 】 浏览:135
Tags:Remove Nth Node From End List

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;
}
};

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Generate Parentheses(C++) 下一篇Sum(C++) - 编程开发习题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目