【转载请注明】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. Linux自动化之Cobbler补鞋匠安装

    cobbler介绍:   快速网络安装linux操作系统的服务,支持众多的Linux发行版:Red Hat.   Fedora.CentOS.Debian.Ubuntu和SuSE,也可以支持网络安装w ...

  2. postgres主从配置

    运维开发技术交流群欢迎大家加入一起学习(QQ:722381733) 开始部署postgres主从(如果没不会安装postgres的请去上一个博文中查看) 这里我使用了两台服务器部署 主:192.168 ...

  3. npm和gulp学习

    npm的使用 node Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境,是一种JavaScript语言运行平台,和浏览器这个运行平台是同一个概念. npm np ...

  4. Tensorflow读取csv文件(转)

    常用的直接读取方法实例:#加载包 import tensorflow as tf import os #设置工作目录 os.chdir("你自己的目录") #查看目录 print( ...

  5. BUPT2017 springtraining(16) #2 ——基础数据结构

    题目在这里 A.似乎是个并查集+??? B.10W的范围,似乎可以暴力来一发二分+sort? 但我猜正解可以O(nlogn)? C.单调队列入门题目 #include <cstdio> ] ...

  6. JS判断浏览器类型和屏幕分辨率来调用不同的CSS样式

    代码如下: <!-- if (window.navigator.userAgent.indexOf("MSIE")>=1) { var IE1024="&qu ...

  7. Oracle数据库点滴

    分页查询: SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A WHERE ROWNUM <= 40)W ...

  8. ubuntu 配置静态路由

    原文:http://blog.sina.com.cn/s/blog_6fd8d5d90101f1xy.html -------------------------------------------- ...

  9. jQuery Validate Ajax 验证

    jQuery Validate Ajax 验证 <script type="text/javascript"> $(function() { $('#formCityL ...

  10. 超简洁代码实现CircleImageView

    效果图: 页面代码: public class CircleView extends ImageView { private Paint mPaint = new Paint(); public Ci ...