LeetCode 019 Remove Nth Node From End of List
题目描述: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,
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.
Try to do this in one pass.
分析:
题目中说n是合法的,就不用对n进行检查了。用标尺的思想,两个指针相距为n-1,后一个到表尾,则前一个到n了。
① 指针p、q指向链表头部;
② 移动q,使p和q差n-1;
③ 同时移动p和q,使q到表尾;
④ 删除p。
(p为second,q为first)
代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) { if(head == NULL || head->next == NULL) return NULL; ListNode * first = head;
ListNode * 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;
}
};
Java:
public ListNode removeNthFromEnd(ListNode head, int n) { if (head == null || head.next == null) {
return null;
} ListNode first = head;
ListNode 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;
}
LeetCode 019 Remove Nth Node From End of List的更多相关文章
- 【JAVA、C++】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 ...
- [Leetcode][019] Remove Nth Node From End of List (Java)
题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointer ...
- 【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 ...
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 【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 ...
- 【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 ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- Java [leetcode 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 ...
- Leetcode 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 ...
随机推荐
- python实现密码破解
排列组合(破解密码) 关注公众号"轻松学编程"了解更多. 1.排列 itertools.permutations(iterable,n) 参数一:要排列的序列, 参数二:要选取的个 ...
- [Luogu P2824] [HEOI2016/TJOI2016]排序 (线段树+二分答案)
题面 传送门:https://www.luogu.org/problemnew/show/P2824 Solution 这题极其巧妙. 首先,如果直接做m次排序,显然会T得起飞. 注意一点:我们只需要 ...
- 团灭 LeetCode 股票买卖问题
很多读者抱怨 LeetCode 的股票系列问题奇技淫巧太多,如果面试真的遇到这类问题,基本不会想到那些巧妙的办法,怎么办?所以本文拒绝奇技淫巧,而是稳扎稳打,只用一种通用方法解决所用问题,以不变应万变 ...
- 10万用户一年365天的登录情况如何用redis存储,并快速检索任意时间窗内的活跃用户
1.redsi的bitmap数据结构介绍 bitmap本质上是一个string类型,只是他操作的是string的某个位是0还是1. setbit和getbit 两条命令是对字符串的位操作.每个位只能是 ...
- CF1303G Sum of Prefix Sums
点分治+李超树 因为题目要求的是树上所有路径,所以用点分治维护 因为在点分治的过程中相当于将树上经过当前$root$的一条路径分成了两段 那么先考虑如何计算两个数组合并后的答案 记数组$a$,$b$, ...
- Java字符串到数组的转换--最后放大招
本文是关于如何在Java中以不同方式将String转换为String Array的几种方法,按照惯例,文末会分享Groovy语言中的实现. split()方法 字符串api是通过split()方法添加 ...
- 有奖体验 CODING 产品,iPad Pro、HHKB 键盘等超级礼包等你来!
DevOps 研发效能升级.高效率研发工具已成为软件研发行业的热门话题,也是每个企业研发团队需要不断探索的命题.CODING 一站式软件研发管理工具平台旨在让开发团队低门槛使用 DevOps 工具,帮 ...
- javascript多物体运动案例:多物体淡入淡出
javascript多物体运动案例:多物体淡入淡出 任务描述: 补充代码,当鼠标移入红色区域时,该区域透明度逐渐增加至不透明;当鼠标移出该红色区域时,该区域透明度逐渐恢复至初始程度. 效果图: < ...
- python之《tkinter》
1.创建窗口 import tkinter as tk window = tk.Tk() window.title('my window') window.geometry('300x100') -- ...
- python之迭代锁与信号量
如果现在需要在多处加锁大于等于2的时候 因为计算机比较笨,当他锁上一把锁的时候又所理一把锁,等他来开锁的时候他不知道用哪把钥匙来开锁, 所以这个时候我们需要把把平常的锁变为迭代锁 eg: import ...