【LeetCode】19. Remove Nth Node From End of List
题目:
思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1。
1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表
扫过的节点依次:1-2-3
n值得变化:3-2-1
2.链表1->2->3,n=3
扫过的节点依次:1-2-3
n值得变化:2-1-0
3.链表1->2->3,n=2
扫过的节点依次:1-2-3
n值得变化:1-0--1
当走到链表结尾时:1.n>0,说明链表根本没有第n个节点,直接返回原链表;
2.n=0,说明链表倒数第n个节点就是头结点,返回head.next;
3.n<0,重新从头结点开始走,没移动一步让n加1,当n=0时,移动停止,当前移动到的节点就是要删除节点的前一个节点。因为如果链表长度是L,则倒数第n个节点的前一个结点就是L-n。第一次扫到链表末尾时,n的值变成n-L,当n不断加1直到为0时,第二次扫到的位置正好是第L-n个节点处。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head==null||n<1){
return head;
}
ListNode cur=head;
while(cur!=null){
n--;
cur=cur.next;
}
if(n==0){
return head.next;
}
if(n<0){
cur=head;
while(++n!=0){//当n=0时移动停止,移动到的节点就是要删除节点的前一个节点
cur=cur.next;
}
cur.next=cur.next.next;
}
return head;
}
}
【LeetCode】19. Remove Nth Node From End of List的更多相关文章
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
- 【LeetCode】019. 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题解(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❤python】 19. Remove Nth Node From End of List
#-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...
- [Leetcode][Python]19: Remove Nth Node From End of List
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...
随机推荐
- erlang远程加载模块须知
erlang加载本地beam到远程节点,需要把依赖库一个个手动加载,否则他不会自动加载. 另外,创建lib的话,使用 rebar-creator create-lib
- -all_load,-ObjC,-force_load三者的区别
参考:https://developer.apple.com/library/mac/qa/qa1490/_index.html,http://www.cnblogs.com/YouXianMing/ ...
- 个人卡付款sql
select arap_djfb.billdate as 单据日期, bd_corp.unitname as 付款单位, arap_djfb.zy as 付款摘要, bd_cubasdoc.custc ...
- Android APP高效开发的十大建议
在使用Android开发APP过程中,为什么确保最优化.运行流畅且不会使Android系统出现问题至关重要呢?因为影响APP产品效率的每一个问题,如:耗电或内存占用情况等,都是关乎APP成功与否关键因 ...
- mysql事件
文章参考自http://blog.163.com/duanpeng3@126/blog/static/8854373520105182123112/在使用这个功能之前必须确保event_schedul ...
- Scala控制抽象
private def filesHere = (new java.io.File(".")).listFiles() def filesEnding(query: String) ...
- about_并查集
前天刚学了并查集,挺好用的,虽然我现在只会用它来解决是不是亲戚啊,是不是朋友啊,带权并查集还不是很理解. 并查集也叫做不相交集合,主要有3个操作,初始化,查找,合并. 并查集其中一个很大的应用就是kr ...
- android之location02
package com.example.mars_3300_location02; import java.net.ContentHandler; import java.util.List; imp ...
- 配置sql server 2000以允许远程访问 及 连接中的四个最常见错误
地址:http://www.cnblogs.com/JoshuaDreaming/archive/2010/12/01/1893242.html 配置sql server 2000以允许远程访问适合故 ...
- Ubuntu下启动Apache的Rewrite功能
在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available/rewrite.l ...