【转载请注明】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. Vue.js:使用vue-cli快速构建项目

    vue-cli是什么? vue-cli 是vue.js的脚手架,用于自动生成vue.js模板工程的. vue-cli怎么使用? 安装vue-cli之前,需要先安装了vue和webpack,不知道怎么安 ...

  2. isset在php5.6-和php7.0+的一些差异

    今天在公司实现一个模块功能时写了如下代码: class ProductCategory { const TYPES = [ 1 => 'type1', 2 => 'type2', ]; p ...

  3. ClassCastException:ColorDrawable cannot be cast to RoundRectDrawableWithShadow

    错误信息 java.lang.ClassCastException: android.graphics.drawable.ColorDrawable cannot be cast to android ...

  4. 清北学堂模拟赛d1t3 听音乐(music)

    题目描述 LYK喜欢听音乐,总共有n首音乐,有m个时刻,每个时刻LYK会听其中一首音乐,第i个时刻会听第ai首音乐.它给自己定了一个规定,就是从听音乐开始,听的每连续n首音乐都是互不相同的.例如当n= ...

  5. hive计算日期差

    首先,hive本身有一个UDF,名字是datediff.我们来看一下这个日期差计算的官方描述,(下面这个是怎么出来的): hive> desc function extended datedif ...

  6. nyoj_448_寻求最大数_201402261424

    寻找最大数 时间限制:1000 ms  |           内存限制:65535 KB 难度:2   描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920 ...

  7. [bzoj4796][CERC2016]Key Knocking_乱搞

    Key Knocking bzoj-4796 CERC-2016 题目大意:描述没有题面短系列..题目链接 注释:$1\le n\le 10^5$. 想法: 乱搞稳AC.考试的时候调试信息又一次杀死了 ...

  8. HDU 4514

    真是神奇,G++TLE,C++500MS... 判环有一个图论知识就是,m>=n时必有环.如果以m的范围建图,会MLE. 然后,利用拓扑排序再来判定是否有环,因为有些景点可能是孤立的.同时,在拓 ...

  9. 极光推送案例-PushExample-Jpush

    ssh - maven - java项目-极光注冊id完毕推送 这是我学习时的步骤: 1:去极光推送平台注冊账号,自己能够去注冊(一般公司会帮助完毕注冊) 地址:https://www.jpush.c ...

  10. JS推断是否为JSON对象及是否存在某字段

    $.ajax({ type: 'POST', url: url, success(function(data){ //推断是否为JSON对象 if(typeof(data) == "obje ...