25.Remove Nth Node From End of List(删除链表的倒数第n个节点)
Level:
Medium
题目描述:
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
思路分析:
题目要求删除链表的倒数第n个节点,首先我们需要判断n是否在有效范围,然后我们计算出链表的长度,求倒数第n个,就是求正数第len-n个。
代码:
public class ListNode{
int val;
ListNode next;
public ListNode(int x ){
val=x;
}
}
public class Solution{
public ListNode removeNthFromEnd(ListNode head,int n){
if(head==null)
return null;
int len=0;
ListNode pNode=head;
while(pNode!=null){
len++;
pNode=pNode.next;
}
if(n>len)
return null;
if(len==1&&len==n)
return null;
if(len==n)
return head.next;
ListNode pNode2=head;
for(int i=0;i<len-n-1;i++){
pNode2=pNode2.next;
}
pNode2.next=pNode2.next.next;
return head;
}
}
25.Remove Nth Node From End of List(删除链表的倒数第n个节点)的更多相关文章
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- [LeetCode]19. 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: Given l ...
- 019 Remove Nth Node From End of List 删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点并返回头结点.例如,给定一个链表: 1->2->3->4->5, 并且 n = 2.当删除了倒数第二个节点后链表变成了 1->2 ...
- 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)
这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...
- Leetcode19.Remove Nth Node From End of List删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)
题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了 2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- [leetcode]19. 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: Given l ...
随机推荐
- Perl基础语法
一.脚本文件perl 代码可以写在一个文本文件中,以 .pl..PL 作为后缀.文件名可以包含数字,符号和字母,但不能包含空格,可以使用下划线(_)来替代空格.一个简单的Perl 文件名:rurun_ ...
- https://github.com/ildoonet/tf-pose-estimation
https://github.com/ildoonet/tf-pose-estimation
- opennebula 模板参数说明
两种模板配置方式一.光驱引导启动需要配置:disk1:磁盘类型:cdrom 驱动类型:file 磁盘标记:hd 是否只读:yesDisk2:磁盘类型:DATABLOCK驱 ...
- 4-fiddler抓包中文乱码:
接受到的html被压缩了,要解压,在工具栏有decode
- 2-python代码坑点
#切片: # L = ['aaa', 'bbb', 'ccc', 'ddd'] # print(L[1 : 3]) #取[1, 3):下标 # L = list(range(100)) # print ...
- Hadoop完全分布式环境搭建(二)——基于Ubuntu16.04设置免密登录
在Windows里,使用虚拟机软件Vmware WorkStation搭建三台机器,操作系统Ubuntu16.04,下面是IP和机器名称. [实验目标]:在这三台机器之间实现免密登录 1.从主节点可以 ...
- ubuntu opencv的使用
博客转载自:https://blog.csdn.net/u012816621/article/details/51732932 CMakeLists.txt # cmake needs this li ...
- Yii2邮箱发送与配置
1配置邮箱 在 common/config/web.php中写入以下代码配置 Mail代理 return [ 'components' => [ ...//your code, //以下是 ma ...
- 利用Thread.stop完成方法执行超时中断
示例代码可以从github上获取 https://github.com/git-simm/simm-framework.git 接上篇博客<FutureTask子线程取消执行的状态判断> ...
- javascript总结9:JavaScript三目运算符
1 三元表达式: 表达式?结果1:结果2: 如果表达式结果为true,执行结果1,如果表达式结果为false,执行结果2. 可以理解为if else 的另外一种写法. 例: var m = 10; ...