思路1:设置两个指针p1,p2指向表头,p1先走n步。再两个指针同时走。当p1指针指到链表尾部时,P2指针已经在需要删除节点的前一位。一定要注意一些细节。

 class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
} public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null||n<=0){
return head;
}
ListNode p1=head;
ListNode p2=head; while(n!=0){
if(p1.next!=null){
p1=p1.next;
}else if(n==1){
return head.next;
}else{
return head;
}
n--;
} //两个指针同时移动
while(p1.next!=null){
p1=p1.next;
p2=p2.next;
}
//此时p2指针已经在倒数第n+1位,开始删除倒数第n位
p2.next=p2.next.next;
return head;

思路2:指针p1向前走n-1步,两个指针同时走,p1走到尾节点时,p2到达的节点就是需要删除的节点。此时,可以通过复制后一个元素val给前一个元素,再删除后一个元素即可。实现删除该元素。 要注意细节。

    public ListNode removeNthFromEnd1(ListNode head, int n) {
if(head==null||n<=0){
return head;
}
if(n==1){
//如果删除倒数第一位,则直接删除
if(head.next==null){
return null;
}else{
ListNode h=head;
while(h.next.next!=null){
h=h.next;
}
h.next=null;
return head;
} }
ListNode p1=head;
ListNode p2=head; while(n!=1){
if(p1.next!=null){
p1=p1.next;
}else if(n!=1){ //如果n大于链表长度的情况
return head;
}
n--;
} //两个指针同时移动
while(p1.next!=null){
p1=p1.next;
p2=p2.next;
}
//此时p2指针已经在倒数第n位,开始删除倒数第n位
p2.val=p2.next.val;
p2.next=p2.next.next;
return head;
}

【Leetcode-easy】Remove Nth Node From End of List的更多相关文章

  1. 【LeetCode OJ】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: G ...

  2. 【Leetcode】【Easy】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 ...

  3. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  4. 【leetcode】Remove Nth Node From End of List(easy)

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

  5. 【leetcode】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 ...

  6. 【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:        ...

  7. 【leetcode刷题笔记】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 ...

  8. LeetCode题解(19)--Remove Nth Node From End of List

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the  ...

  9. LeetCode OJ 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 ...

  10. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

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

随机推荐

  1. EasyMvc入门教程-基本控件说明(12)栏目导航

    栏目导航一般用来显示当前页面所在的模块层级位置关系,如下图所示: 当然也有前端网站作为小栏目导航,凡是没有绝对,只要不违和就好:),下面上代码: @{ var data = new List<N ...

  2. (转)ubuntu/var/log/下各个日志文件

    本文简单介绍ubuntu/var/log/下各个日志文件,方便出现错误的时候查询相应的log   /var/log/alternatives.log-更新替代信息都记录在这个文件中 /var/log/ ...

  3. Map接口及其子类

    Map接口操作的是一对对象,即二元偶对象,Map接口中的每一个元素都使用"key--value"的形式存储在集合中. SortedMap接口是排序接口,仅仅要是实现了此接口的子类, ...

  4. 一款炫酷Loading动画--载入成功

    简单介绍 昨天在简书上看到一篇文章.介绍了一个载入动画的实现过程 一款Loading动画的实现思路(一) 仅仅可惜原动画是IOS上制作的.而看了一下.作者的实现思路比較复杂,于是趁着空暇写了一个And ...

  5. JS创建表单提交备份

    //保存 function saveFT() { var data = { createDate: GetDateStr(0), name: $("#txtName").val() ...

  6. sql数据库log自动增长被取消

    原因分析:数据库可分配空间为0 解决方法:增加数据库初始大小

  7. Flash威胁的不不过浏览器

    Adobe为提升Flash的安全性.在最新版本号的Flash(18.0.0.209)增加了很多攻击缓解技术. 新的攻击缓解技术为: l  <*>长度验证–添加长度cookie到Vector ...

  8. typedef struct与struct定义结构体

    今天在定义结构体的时候发现typedef struct与struct定义结构体有一些不同之处: 结构也是一种数据类型, 能够使用结构变量, 因此,  象其他 类型的变量一样, 在使用结构变量时要先对其 ...

  9. python爬虫,从hao123爬取网址信息

    最近研究python的爬虫,小小程序,拿下来分享,本人使用python3.7,纯粹兴趣爱好,希望能帮助大家激发兴趣.从hao123,爬取各种网址信息,代码如下. import urllib.reque ...

  10. Microsoft-office 常见问题

    1.工作表写入保护,忘记密码,解决办法: 流程如下: 1打开文件2工具---宏----录制新宏---输入名字如:aa3停止录制(这样得到一个空宏)4工具---宏----宏,选aa,点编辑按钮5删除窗口 ...