leetcode 19

最开始用一般的方法,首先遍历链表求出长度,进而求出需要删除节点的位置,最后进行节点的删除。
代码如下:
/**
* 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) {
ListNode* h = head;
int length = ;
while(h != NULL)
{
length ++;
h = h->next;
}
int s = length - n;
if(s == )
{
return head->next;
}
ListNode* hh = head;
s--;
while(s > )
{
hh= hh->next;
s--;
}
h = hh->next;
hh->next = h->next;
return head;
}
};
之后学习了别人更加巧妙的方法:
同时初始化两个指向头结点的指针,一个先向后走n步,然后两个指针同时向后移动,
当第一个指针到达终点的时候第二个指针的位置就是要删除的结点。
/**
* 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) {
ListNode* head1 = head;
ListNode* head2 = head;
while(n > )
{
head1 = head1->next;
n --;
}
if(head1 == NULL)
{
return head->next;
}
while(head1->next != NULL)
{
head1 = head1->next;
head2 = head2->next;
}
head1 = head2->next;
head2->next = head1->next;
return head;
}
};
重写后提交竟然是100%~

leetcode 19的更多相关文章
- [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 ...
- Java实现 LeetCode 19删除链表的倒数第N个节点
19. 删除链表的倒数第N个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当 ...
- LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)
题目链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/?tab=Description Problem: 移除距离 ...
- [leetcode] 19. Count and Say
这个还是一开始没读懂题目,题目如下: The count-and-say sequence is the sequence of integers beginning as follows: 1, 1 ...
- LeetCode 19——删除链表的倒数第N个节点(JAVA)
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...
- 每日一道 LeetCode (19):合并两个有序数组
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- [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, ...
- Leetcode 19 Remove Nth Node From End of List 链表
删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...
- 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 ...
随机推荐
- suibi 117
-Djava.net.preferIPv4Stack=true re.findall(r"^(\d+)" , content) content = content.replace( ...
- Axis2的下载和安装
Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持S ...
- Java日期时间使用总结
一.Java中的日期概述 日期在Java中是一块非常复杂的内容,对于一个日期在不同的语言国别环境中,日期的国际化,日期和时间之间的转换,日期的加减运算,日期的展示格式都是非常复杂的问题. 在J ...
- [ActionScript 3.0] AS3 对XML的操作,创建、删除、增加节点方法
package { import flash.display.Sprite; /** * @author:Frost.Yen * @E-mail:871979853@qq.com * @create: ...
- rsync 目录 斜杠
源: 不带:同步 目录和内容 带/: 只同步内容 target目录: 待. -Warv --delete -W, --whole-file copy files whole ( ...
- js中格式化时间字符串
.net 程序员肯定有遇到过,将一个对象json序列化之后Date 字段 就会转化成 '/Date(1370770323740)/' 这种格式的数据,下面介绍一种在js中,关于时间格式的转换. < ...
- spring-aop示例
具体案例放在github上,主要是jar包在上面 https://github.com/guoyansi/spring-aop-example knights.xml <?xml version ...
- 关于BEA-000402和BEA-000438
OS:rh5 64位 JDK:1.5 64位 weblogic:9.2.3 jar 应用程序部署后,启动受管服务器报如下警告和错误: 这个问题导致系统性能下降,打开weblogic控制台各项功能和应用 ...
- flash 读取系统默认编码
java有类可以直接读取,但貌似flash没有. Charset.defaultCharset(); 但是浏览器里可以有. document.defaultCharset;//从当前的区域语言中获取默 ...
- 在git上下载的Asp.Net MVC 4源码怎么编译?
以本人的下载位置为例:E:\aspnetwebstack 1.win+r 输入cmd 打开dos 界面 2.e: 回车,定位到e 盘 3.cd E:\aspnetwebstack 进入e 盘aspne ...