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 ...
随机推荐
- do break的妙用
#include <stdio.h> #include <malloc.h> int func(int n) { //资源的统一申请 ; ; int* p = (int*)ma ...
- iis7设置404页面不生效的原因
打开web.config <httpErrors errorMode="Custom" existingResponse="PassThrough"> ...
- 基于jquery的表单校验插件 - formvalidator使用体验
下载地址:http://www.formvalidator.net/ 基本样例 <form action="/registration" method="POST& ...
- linux安装iscsi target,make时出错,解决方法
安装主要是按照这个网址的步骤来的:http://ixdba.blog.51cto.com/2895551/526452 执行到make步骤时,出错: root@host:~/iscsitarget-1 ...
- 用block响应button的点击事件
1.继承UIButton : 2.在自己定义的button类中的方法 addTarget:(id)target action:(SEL)action forControlEvents:(UIContr ...
- 【转】深入理解DIP、IoC、DI以及IoC容器
原文链接:http://www.cnblogs.com/liuhaorain/p/3747470.html 前言 对于大部分小菜来说,当听到大牛们高谈DIP.IoC.DI以及IoC容器等名词时,有没有 ...
- CRM 2016 自定义lookup过滤
function preFilterLookup() { //终端业态 Xrm.Page.getControl("new_typeofoperationid").addPreSea ...
- oracle 导入数据
1.在数据库中建立实例数据库之后,运行cmd 2.键入 imp空格(实例数据库名)/(实例数据库口令)空格file=“拖入数据地址” 比如czt.dmp文件直接拖进去(空格)full=y 3.按ent ...
- jQuery formValidator表单验证插件常见问题
1. 如何实现一个控件,根据不同的情况,实现不同的控制? 2. 一个页面上我有几个tab页,如何实现每个Tab页上的控件单独校验? 3. 我采用的页面上文字问题的方式,点提交的时候, ...
- 如何让Form窗体接收KeyDown事件?
在使用.Net Framework编写窗体应用程序的时候,有时有需要响应窗体的按键消息.当窗体上没有任何其他控件的时候,窗体是可以直接响应这些消息的. 但是当窗体上有其他控件时,会发现窗体再也不会响应 ...