删除链表的倒数第N个节点(java实现)
题目:
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
示例:
给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 1->2->3->5.
说明:
给定的 n 保证是有效的。
进阶:
你能尝试使用一趟扫描实现吗?
看到这个题,我们得有一些思路:
1.删除的那个节点需要找到,那就需要双指针pre和index,
2.双指针开始都是指向头节点,index先走到比头节点的大n的地方,
3.然后两个指针同时往后遍历,当index的下一个为空时,此时pre指向的就是需要删除的那一个节点,
4.删除的这个节点还需要分情况:(1)、它的后续节点不为空,(2)、它的后续为空,
5.若后续不为空就需要后面的去覆盖它,若为空,就只需要设置为null就可以了。
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(null == head){
return null;
}
if(n <= 0){
return head;
}
ListNode pre = head;
ListNode index = head;
ListNode orghead = head;
for(int i = 1;i < n ;i++){ //让i先走n步
index = index.next;
if(null == index){
return null;
}
}
while(null != index.next){ //到n的地方,pre也开始遍历
orghead = pre;
pre = pre.next;
index = index.next;
}
//删除时得考虑删除节点是否有后续节点
if(null != pre.next){
pre.val = pre.next.val; //有就往前移
pre.next = pre.next.next;
}else{
if(null == pre.next){ //没有后续,只需把它设置为空就可以
return null;
}
orghead.next = null;
}
return head;
}
}
我测试了一下,不判断它的后续节点是否为空,直接赋值,出现了空指针异常。
不考虑后续节点的代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre = head;
ListNode index = head;
for(int i = 1;i < n;i++){
index = index.next;
if(index == null){
return null;
}
}
while(index != null){
index = index.next;
pre = pre.next;
}
pre.next = pre.next.next;
return head;
}
}
出现的异常:

删除链表的倒数第N个节点(java实现)的更多相关文章
- leetcode 19. 删除链表的倒数第N个节点 JAVA
题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链 ...
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- 删除链表中倒数第n个节点
给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1 ...
- [LeetCode] 19. 删除链表的倒数第N个节点
题目链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/ 题目描述: 给定一个链表,删除链表的倒数第 n 个节点, ...
- [Swift]LeetCode19. 删除链表的倒数第N个节点 | Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 22、删除链表的倒数第N个节点
22.删除链表的倒数第N个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删 ...
- 0011 删除链表的倒数第N个节点
给 定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- 删除链表的倒数第N个节点
题目描述: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后 ...
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以访问我的 github. 描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回 ...
随机推荐
- 一张图解释IaaS,PaaS,SaaS
图片来源于MVA教程:快速入门——面向IT专业人员的Windows Azure IaaS
- 000-SQL Server
MyWeb数据库,cantus表 USE [MyWeb]GO /****** Object: Table [dbo].[cantus] Script Date: 2017/12/12 12:12:12 ...
- windows下redis 配置文件参数说明
1.先看redis.windows.conf 文件 # Redis configuration file example # Note on units: when memory size is ne ...
- REST framework---基于类的视图
一.程序设计 1.路由设计 from django.conf.urls import url from django.contrib import admin from app import view ...
- vue生命周期钩子函数
<template> <div> <h2>this is from C.vue</h2> </div> </template> ...
- flask 电子邮件Flask-Mail
电子邮件 在web程序中,经常会需要发送电子邮件.比如,在用户注册账户时发送确认邮件:定期向用户发送热门内容或是促销信息等等.在Web程序中发送电子邮件并不复杂,借助扩展Flask-Mail或是第三方 ...
- 中文编码错误,Error output could not be translated from the native locale to UTF-8.
假如使用http访问仓库,用户配置的pre-commit钩子里面如果有中文,可能会出现"Error output could not be translated from the nativ ...
- datagridview 添加数据库数据
private void btnadd_Click(object sender, EventArgs e) { string str = @"Data Source=(localdb)\MS ...
- c# 调用RDP和SSH实现远程登陆
1.ssh的登陆实现: windows平台可以安装OpenSSHforWindows 后,可以通过cmd 执行ssh的指令. 也可以在c#编程中实现ssh的登陆: var p = new System ...
- leetcode 二进制求和 python
class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str ...