019 Remove Nth Node From End of List 删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点并返回头结点。
例如,
给定一个链表: 1->2->3->4->5, 并且 n = 2.
当删除了倒数第二个节点后链表变成了 1->2->3->5.
详见:https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
思路:设置两个指针first跟second。first指针先移动n步,若此时first指针为空,则表示要删除的是头节点,此时直接返回head->next即可。如果first指针不为空,则将两个指针一起移动,直到first指针指向最后一个节点,令second->next=second->next->next即可删除第你n个节点。
实现语言:Java
/**
* 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(head==null||head.next==null){
return null;
}
ListNode first=head,second=head;
for(int i=0;i<n;++i){
first=first.next;
}
if(first==null){
return head.next;
}
while(first.next!=null){
first=first.next;
second=second.next;
}
second.next=second.next.next;
return head;
}
}
参考:https://blog.csdn.net/yao_wust/article/details/41242805
019 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 ...
- 【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 ...
- [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 ...
随机推荐
- Poj1159 Palindrome(动态规划DP求最大公共子序列LCS)
一.Description A palindrome is a symmetrical string, that is, a string read identically from left to ...
- Boost库之asio io_service以及run、run_one、poll、poll_one区别
一.io_service的作用 io_servie 实现了一个任务队列,这里的任务就是void(void)的函数.Io_servie最常用的两个接口是post和run,post向任务队列中投递任务,r ...
- influxdb api
https://influxdb-python.readthedocs.io/en/latest/examples.html
- C# 删除文件和目录到回收站
if (Directory.GetDirectories(projectPath).Length == 0 && Directory.GetFiles(projectPath).Len ...
- TCP/IP以及Socket对象基本
1 OSI七层模型概念介绍 物理层:数据以比特的方式进行传递,典型的设备是集线器.该层主要规定了设备的电压或者端口等等一些列物理层面上的规定 数据链路层:该层数据以帧的方式进行传递,主要是两个 ...
- assert.fail()
assert.fail(message) assert.fail(actual, expected[, message[, operator[, stackStartFunction]]]) oper ...
- ObservableCollection 分组后排序报错问题
ObservableCollection通过Move方法可以移动顺序,如下: 将ObservableCollection中的一个item置顶: private ObservableCollection ...
- 8、SRR数据下载https://ftp-trace.ncbi.nlm.nih.gov/sra/sdk/2.8.2/
1.prefetch SRRxxxxxx -/ncbi/public/sra 2.fastq-dump --split-files xxxxxxsra 3.SRA.SAM以及Fastq ...
- sklearn正规化(Normalization或者scale)
from sklearn import preprocessing import numpy as np a = np.array([[10,2.7,3.6],[-100,5,-2],[120,20, ...
- storm定时器timer源码分析-timer.clj
storm定时器与java.util.Timer定时器比较相似.java.util.Timer定时器实际上是个线程,定时调度所拥有的TimerTasks:storm定时器也有一个线程负责调度所拥有的& ...