【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 ...
随机推荐
- dedecms安装完成后登录后台出现空白
今天家里的电脑上新装DEDE5.7后台登陆后竟然一片空白经过分析代码,发现登录验证的时候用到了这个函数session_register(我的环境:win7+PHP5.4.3+Apache2.2.22+ ...
- django博客开发
找一文件夹作为项目文件夹1 django-admin.py startproject mysite建立工程2 cd mysite python manage.py startapp blog 建立第一 ...
- NVIDIA® Quadro® 四路缓冲 3D立体方案
http://www.nvidia.cn/object/quadro_pro_graphics_boards_cn.html NVIDIA® Quadro® 专业显卡让地球学家以及时装设计师等许多专业 ...
- C# Graphics
Graphics.FillPie 方法 填充由一对坐标.一个宽度.一个高度以及两条射线指定的椭圆所定义的扇形区的内部. Graphics.FillPie (Brush, Int32, Int32, I ...
- AngularJS的稍复杂form验证
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsSoccerFormCheck.rar 代码: <!DOCTYPE HTM ...
- Shell脚本之:数组
bash支持一维数组,并且没有限定数组的大小,数组元素的下标由0开始编号. 定义数组 在Shell中,用括号来表示数组,数组元素用“空格”符号分割开.定义数组的一般形式为: array_name=(v ...
- iOS - 贝塞尔曲线,折线,曲线,波浪线
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZHlsYW5fbHdiXw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- RelativeLayout布局(仅在RelativeLayout中有效)
在父亲布局的相对位置 android:layout_alignParentLeft="true" //在布局左边 android:layout_alignParentRig ...
- Android · 获取网络图片
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...
- Python数据结构:列表、元组和字典
在Python中有三种内建的数据结构——列表list.元组tuple和字典dict 列表中的项目包括在方括号中,项目之间用逗号分割 元组和列表十分类似,只不过元组和字符串一样是不可变的 即你不能修改元 ...