思路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. php数据库操作代码

    数据库名为reg,表名为member,字段名为username,password,regdate <?php $link=@mysql_connect("localhost" ...

  2. logback MDC(Mapped Diagnostic Context)与分布式系统的跟踪系统

    logback MDC(Mapped Diagnostic Context)与分布式系统的跟踪系统 logback官方文档中第8章Mapped Diagnostic Context给我们提供了一些分布 ...

  3. 先行发生原则(Happens-before)

    先行发生原则(Happens-Before)是判断数据是否存在竞争.线程是否安全的主要依据. 先行发生是Java内存,模型中定义的两项操作之间的偏序关系,如果操作A先行发生于操作B,那么操作A产生的影 ...

  4. POJ 3181 Dollar Dayz(全然背包+简单高精度加法)

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  5. Libimseti推荐系统

    技术:easyUI.jQuery.Spring.Struts.Hibernate.Mahout.MySQL 本Libimseti推荐系统使用数据.代码參考<Mahout in action> ...

  6. 我用select做多路复用踩到的坑

    既然说是用select踩到的坑,那么就先直接贴一段使用select的代码上来瞅一下: bool SocketAction(int fd, const char* buf, size_t len, ui ...

  7. 值得推荐的android开发框架简单介绍

    一些总结出来的Android高速开发框架,所有都是开源框架,附带项目地址,是开发学习的绝佳资料. Direct-Load-apk项目 项目主页地址:http://www.kymjs.com/ 功能:D ...

  8. 【Python】字典~深入篇

    字典的定义 字典是一系列键值对,字典用放在{}一系列键值对表示 info = {','city':'KunMing'} 字典增.删.改.查 增加新元素 指定字典名,用方括号括起来的键和相关的值 inf ...

  9. Objective-C 执行AppleScript脚本

    在Objective-C里事实上也能够执行AppleScript 第一种方式是Source 将脚本写到变量字符串里 NSAppleEventDescriptor *eventDescriptor = ...

  10. java拷贝构造函数

    浅拷贝就是指两个对象共同拥有同一个值,一个对象改变了该值,也会影响到另一个对象. 深拷贝就是两个对象的值相等,但是互相独立. 构造函数的参数是该类的一个实例.   Operator = 拷贝构造函数 ...