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 ...
随机推荐
- 优化studio的速度
随着Android Studio开发工具的逐渐成熟,越来越多的程序员选择这种IDE工具来进行开发,但是android studio在使用过程中有时候会出现卡顿问题.在赶项目的时候,遇到这类问题最是苦恼 ...
- Android之BroadcastReceiver 监听系统广播
绑定广播有两种方式 一.配置文件绑定,在程序未启动也能监听 二.代码方式绑定,在程序启动后才能监听 1.绑定和取消绑定广播 public class MainActivity extends Acti ...
- 配置sql server 2000以允许远程访问 及 连接中的四个最常见错误
地址:http://www.cnblogs.com/JoshuaDreaming/archive/2010/12/01/1893242.html 配置sql server 2000以允许远程访问适合故 ...
- 配置HylaFAX传真服务器
配置HylaFAX传真服务器转自 http://blog.chinaunix.net/uid-8551991-id-248081.html参考:http://www.hylafax.org/howto ...
- 20145305 《Java程序设计》第8周学习总结
教材学习内容总结 1.NIO使用频道来衔接数据节点,可以设定缓冲区容量,在缓冲区中对感兴趣的数据区块进行标记,提供clear().rewind().flip().compact()等高级操作 2.想要 ...
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
做一个map和字符串的转换,需要导入这些jar包,这是最基本的一些jar包. 经过多方尝试得出结论入下: 首先导入基本包:json-lib-2.2.3-jdk15.jar 如果没有这个jar包,程序是 ...
- 九度OJ1008
这道题其实就是一个简单的Dijkstra变形,只是要考虑重边的情况. 当在更新图中点到起点的距离时,将花费p也计算在内:如果长度与之前计算的值相等,则再考虑此时花费p是否会更少,是的话则仍然要更新最短 ...
- C# 中将多个空格替换成一个空格
2013-04-17 15:36 C# 中如何将多个空格替换成一个空格? 1 myString = Regex.Replace(myString, @"\s+", " & ...
- (整理)RPC
今天研究了一下Webservice,然后就查到RPC,看到CSDN上有两篇很好的文章,就学习一下,并记录在此以便后续研究 http://blog.csdn.net/mindfloating/artic ...
- 什么是条带化(striping) ?(转载)
条带(strip)是把连续的数据分割成相同大小的数据块,把每段数据分别写入到阵列中的不同磁盘上的方法.简单的说,条带是一种将多个磁盘驱动器合并为一个卷的方法. 许多情况下,这是通过硬件控制器来完成的. ...