删除从后往前数的第n个节点

我的做法是将两个指针first,second

先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next->next为空

最后只要删除second->next

 /**
* 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) return head; ListNode* first = head;
for(int i = ; i < n - ; first = first->next, ++i); if(!first->next){
first = head;
head = head->next;
delete first;
return head;
} ListNode* second= head;
for(;first->next->next; first = first->next, second = second->next); ListNode* next = second->next;
second->next = next->next;
delete next; return head;
}
};

Leetcode 19 Remove Nth Node From End of List 链表的更多相关文章

  1. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. [leetcode 19] Remove Nth Node From End of List

    1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  3. Java [leetcode 19]Remove Nth Node From End of List

    题目描述: Given a linked list, remove the nth node from the end of list and return its head. For example ...

  4. Leetcode 19——Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  5. (链表 双指针) leetcode 19. Remove Nth Node From End of List

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  6. [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

  7. 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

  8. [LeetCode] 19. Remove Nth Node From End of List ☆

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. [LeetCode]19. Remove Nth Node From End of List删除链表的倒数第N个节点

    Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...

随机推荐

  1. .NET Remoting原理及应用实例:

    Remoting:(本文摘自百度百科) 简介:        什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方 式.从微软的产品角度来看,可以说Remoting就是DCOM的一种升 ...

  2. 《CSS3秘籍》(第三版)-读书笔记(3)

    第9章 装饰网站导航 1.  选择要定义样式的链接 大部分浏览器都支持4种基本的链接状态:未访问的链接.已访问的链接.访问者的鼠标正悬停在上方的链接,以及正被单击的链接.CSS提供了与这些状态对应的4 ...

  3. 高频交易[z]

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:袁浩瀚链接:https://www.zhihu.com/question/21789812/answer/22178178来源 ...

  4. sqlite中的replace、insert、update之前的区别

    本文转自http://www.ithao123.cn/content-933827.html,在此感谢作者 android数据库操作,有两种方式,一种用android提供给我们的数据库操作函数inse ...

  5. 按enter执行click或者搜索问题

    最近需要将按按钮搜索的功能,改为支持按enter回车键,搜索. 实现按enter,执行click事件还是比较简单的. //按回车搜索用户 $(window).keydown(function (eve ...

  6. [php-src]一个Php扩展的结构

    内容均以php5.6.14为例. 要拥有一个PHP扩展的架子,使用源码中准备好的 /ext/ext_skel 工具,可以生成一个可运行的扩展骨架. 不加选项运行 ./ext_skel,可查看所有可用选 ...

  7. 云计算和大数据时代网络技术揭秘(十三)VXLAN

    Vxlan(virtual Extensible LAN)虚拟可扩展局域网,是一种Overlay方式的网络技术,采用了mac in UDP的方式 进行封装,共50字节的报头.该技术的目标是解决虚拟机在 ...

  8. 团队作业week3

    请每个团队的成员划分各自的角色:PM.Dev.Test,并通过团队博客发布. 请每个团队给出团队贡献分的分配方法,团队贡献分总分数数为:20 * 团队人数.要求每两个人的分数都不相同.请在团队博客写出 ...

  9. JavaScript-setTimeout

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. Winpcap构建用户级网桥

    Winpcap构建网桥 根据winpcap sdk中的user-level-bridge用户级网桥 |机器1                 |                  |机器2   | | ...