【Leetcode-easy】Remove Nth Node From End of List
思路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的更多相关文章
- 【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 ...
- 【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 ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【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 ...
- 【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 ...
- 【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: ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python+tesseract验证码识别的一点小心得
由于公司需要,最近开始学习验证码的识别 我选用的是tesseract-ocr进行识别,据说以前是惠普公司开发的排名前三的,现在开源了.到目前为止已经出到3.0.2了 当然了,前期我们还是需要对验证码进 ...
- 228. 汇总区间(leetcode)
#整体思路:使用堆栈,在Python中可以使用列表代替:如果a[i]-a[i-1]==1,就要将a[i]合并到之前的区间里,#所以我们队首位元素开辟一个区间为[a[0],a[0]]#做最后汇总时候,如 ...
- 计算机图形学OpenGL中的glLoadIdentity、glTranslatef、glRotatef原理,用法 .(转)
单位矩阵 对角线上都是1,其余元素皆为0的矩阵. 在矩阵的乘法中,有一种矩阵起着特殊的作用,如同数的乘法中的1,我们称这种矩阵为单位矩阵. 它是个方阵,除左上角到右下角的对角线(称为主对角线)上的元素 ...
- Java数据结构-线性表之静态链表
静态链表的定义: 节点由一个一维数组和一个指针域组成,数组用来存放数据元素,而指针域里面的指针(又称游标)用来指向下一个节点的数组下标. 这种链表称之为静态链表. 链表中的数组第一个和最后一个位置须要 ...
- ruby 作为嵌入脚本时使用的注意事项
近期一直在採坑... 假设是作为嵌入式脚本使用ruby的话... 一定会遇到这2个问题... gem安装的,无法在嵌入时使用..为啥.? 由于你没require 'ruby gem' 出现 找不到 E ...
- ZooKeeper 系列(一)—— ZooKeeper核心概念详解
一.Zookeeper简介 二.Zookeeper设计目标 三.核心概念 3.1 集群角色 3.2 会话 3.3 数据节点 3.4 节点 ...
- Centos 7.0防火墙问题
从Centos7开始,自带的防火墙从iptables更改成了firewall.一般在企业环境,出于人力和稳定性考虑,还是用成熟的技术比较稳妥. 以下是关闭firewall的方法 systemctl s ...
- Erlang的系统限制
Erlang本身对进程数,原子长度等没有限制,但为了提高性能和节省内存,总会在实际实现中和运行环境中作出一些限制. 1.进程数量 缺省情况下同时存在的最大Erlang进程数量不超过2^18=26214 ...
- 15 nginx反向代理实现nginx+apache动静分离
一:nginx反向代理实现nginx+apache动静分离-------------概念--------------------------- nginx反向代理服务器+负载均衡 用nginx做反向代 ...
- CPI和GDP有什么关系
CPI反映消费价格变化情况,是一个相对数.GDP反映国民经济生产总量,是一个绝对数.CPI的变动反映经济运行过程中物价变动情况,是观察通货膨胀程度的重要指标,GDP的变化则反映经济的增长情况.经济增长 ...