Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy

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

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

/**
* 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) {
int len = ;
ListNode* p = head;
while(p){
len++;
p=p->next;
}
p = head;
ListNode* pre = NULL;
int m = len -n;
while(m){
pre = p;
p=p->next;
m--;
}
if(pre){
pre->next = p->next;
}else{
p = head;
head = head->next;
}
delete(p);
return head;
}
};

最新文章

  1. [转]Java 征途:行者的地图
  2. Hinet 日本数据处理流程
  3. BestCoder Round #74
  4. ARM 嵌入式中断
  5. poj-2393 Yogurt factory (贪心)
  6. 51nod1757 大灾变
  7. BS开发平台,一小时搞定功能强大的统计分析页面
  8. Python3 如何优雅地使用正则表达式(详解七)
  9. 是否使用安全模式启动word
  10. Html5 监听拦截Android返回键方法详解
  11. 原生Eclipse下Java服务器调试的一个问题
  12. vim 复制粘贴
  13. win10 Faster-RCNN训练自己数据集遇到的问题集锦 (转)
  14. java MD5/AES/DES加解密汇总
  15. strchr()的用法
  16. 图->定义
  17. Druid + Grafana 应用实践
  18. 在ASP.NET Web API中使用OData的单例模式
  19. ArcGIS Desktop 10.1+ArcEngine10.1完全破解安装教程(含下载地址+亲测可用!)
  20. org.apache.http 源代码下载

热门文章

  1. 调用打印机Demo
  2. OS X EI Capitan 安装mysql-5.7.9
  3. React-Native之ViewPagerAndroid的使用
  4. 使用SignalR和SQLTableDependency跟踪数据库中记录的变动
  5. 给小班讲stl 之 map、sort、优先队列
  6. Sphinx Makefile
  7. Max Sum(hd P1003)
  8. Spring中的DataBinding(一)
  9. MYSQL 提取时间中的信息的 4 方法
  10. CSS3 设置 Table 隔行变色