【转载请注明】http://www.cnblogs.com/igoslly/p/8672656.html

看一下题目:

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.

题目大意:

给定一个链表,删除这个链表倒数第n个元素

注意:

1、n值有效

2、尝试一遍结束

思路1:

1、先计算链表的总长度,遍历一次

2、得到总长度 length/cnt,计算出倒数第n个元素的位置,再遍历到该位置

实现方法1:

由于题目涉及到 [1],n=1的情况,直接返回NULL,如果依旧使用 ListNode * p = head 的遍历,在语句 p->next=p->next->next; 需要额外添加判断语句,否则会报错;

我们这里额外定义了无意义的 ListNode *res 结果结点,next 指向head,巧妙地避免上类情况,结果 return res->next。

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *res=new ListNode();
res->next=head;
ListNode *p=head;
int cnt=; // 链表计数
while(p){
cnt++;
p=p->next;
} p=res;
// 位置从1-cnt,倒数第n个数,为cnt-n+1
for(int i=;i<cnt-n+1;i++){
p=p->next;
}
p->next=p->next->next;
return res->next;
}
};

思路2:

当然题目要求,这道题自然有遍历一遍的方法

要恰好地得到倒数第n个结点位置,那么需要增加临时指针 *p1 , *p2,使两者保持n的距离

那么当 p1 达到末尾时,p2 即指向被删除结点

class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *res=new ListNode();
res->next=head;
ListNode *p=res;
// head 做先指针,先走n布
while(n--){head=head->next;}
// head和p保持n步距离,直到head到末尾NULL
while(head!=NULL){
head=head->next;
p=p->next;
}
// 删除结点
p->next=p->next->next;
return res->next;
}
};

Leetcode0019--Remove Nth Node From End of List 移除链表第N个结点的更多相关文章

  1. [LeetCode] 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每天一题】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:        ...

  3. 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 ...

  4. [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 ...

  5. LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)

    题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...

  6. 25.Remove Nth Node From End of List(删除链表的倒数第n个节点)

    Level:   Medium 题目描述: Given a linked list, remove the n-th node from the end of list and return its ...

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

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

  8. 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现

    题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二 ...

  9. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

随机推荐

  1. Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)

    传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...

  2. 25.TF&IDF算法以及向量空间模型算法

    主要知识点: boolean model IF/IDF vector space model     一.boolean model     在es做各种搜索进行打分排序时,会先用boolean mo ...

  3. Vue动态组件&异步组件

    在动态组件上使用keep-alive 我们之前曾经在一个多标签的界面中使用is特性来切换不同的组件: Vue.js的动态组件模板 <component v-bind:is="curre ...

  4. Ubuntu 安装有道词典

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51302546 官网首页:有道词典 其中 ...

  5. 清北学堂模拟赛d3t5 c

    分析:其实就是一道数学题.如果以左下角的点为原点建立平面直角坐标系,那么点(b,a)是最容易卡住棺材的.我们求出棺材左边到点(b,a)的距离最小值,只有w小于等于这个最小值才能被拉过去.那么先求出左面 ...

  6. MyBatis 3实现时间段精确的查询(转)

    效果如下: 说明: 时间范围的查询会存在以下问题: 1.如果单纯采用年月日的形式会出现缺少最后一点的数据,比如要查询2015-09-16到2015-09-17,那么2015-09-17 01:00:0 ...

  7. not in 和 <> 不走索引

    首先我们要知道的一点就是CBO的代码oracle是不会对我们公开的,起码现在是.所以本文中的结论不一定适用所有的版本.在应用本文的结论之前最好先试一下. ok 下面就是本文的结论,当你在where语句 ...

  8. servlet 中getLastModified()

    在http协议中,浏览器对访问过的页面缓存后,它将会在以后访问该页面时,将会根据LastModified头字段指定的时间值生成If-Modified-Since头字段,作为缓存页面的最新更新时间.如果 ...

  9. [转]supervisor 安装、配置、常用命令

    原文: http://www.cnblogs.com/xueweihan/p/6195824.html ------------------------------------------------ ...

  10. c#面试题总结

    using System; class A { public A() { PrintFields(); } public virtual void PrintFields(){} } class B: ...