【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 ...
随机推荐
- HDU1969
记得用PI=acos(-1)反三角函数求,用一次排序,然后二分和贪心 #include<iostream> #include<algorithm> #include<io ...
- IDEA使用Maven打包时如何去掉测试阶段
如图
- Spring boot Security Disable security
When I use security.basic.enabled=false to disable security on a Spring Boot project that has the fo ...
- Notification(二)——PendingIntent的flag导致数据同样的问题
MainActivity例如以下: package cc.cu; import android.os.Bundle; import android.view.View; import android. ...
- C++必知必会(1)
条款1数据抽象 抽象数据类型的用途在于将变成语言扩展到一个特定的问题领域.一般对抽象数据类型的定义须要准训下面步骤: 1. 为类型取一个描写叙述性的名字 2. 列出类型所能运行的操作 ...
- UNP学习笔记(第十七章 ioctl操作)
ioctl相当于一个杂物箱,它一直作为那些不适合归入其他精细定义类别的特性的系统接口. 本章笔记先放着,到时候有需要再看 ioctl函数 #include <unistd.h> int i ...
- 安装centos出错
在vitural Box中安装centos,出现了如下问题,重新下一遍就好了,如果网速很慢,下载的过程中总是断断续续的就容易出现下载文件损坏的问题. Could not get the storage ...
- SharePoint 2013 对话框
The quick way to open a sharepoint 2013 dialog modal form is via Javascript below 1 2 3 4 5 function ...
- 【基础算法】排序-复杂排序之二(找出第K大的数)
切割的思想是高速排序最精髓的地方.每一次切割出来的元素K一个排在第K位,所以利用这样的思想我们至少知道3点 1. 被切割出来的元素K最后一定排在第K位. 2. 在K左边的元素一定比K小或者相等. 3. ...
- warning: mysql-community-libs-5.7.11-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5
1.错误描写叙述 [root@ mysql]# rpm -ivh mysql-community-libs-5.7.11-1.el7.x86_64.rpm warning: mysql-communi ...