最开始用一般的方法,首先遍历链表求出长度,进而求出需要删除节点的位置,最后进行节点的删除。

代码如下:

 /**
* 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的更多相关文章

  1. [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 ...

  2. Java实现 LeetCode 19删除链表的倒数第N个节点

    19. 删除链表的倒数第N个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当 ...

  3. LeetCode 19 Remove Nth Node From End of List (移除距离尾节点为n的节点)

    题目链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/?tab=Description   Problem: 移除距离 ...

  4. [leetcode] 19. Count and Say

    这个还是一开始没读懂题目,题目如下: The count-and-say sequence is the sequence of integers beginning as follows: 1, 1 ...

  5. LeetCode 19——删除链表的倒数第N个节点(JAVA)

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 ...

  6. 每日一道 LeetCode (19):合并两个有序数组

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  7. [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, ...

  8. Leetcode 19 Remove Nth Node From End of List 链表

    删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...

  9. 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 ...

随机推荐

  1. JS常用各种正则表达式

    1.非负整数         /^\d+$/ 2.正整数            /^[0-9]*[1-9][0-9]*$/ 3.非正整数        /^((-\d+)|(0+))$/ 4.负整数  ...

  2. mapreduce任务中Shuffle和排序的过程

    mapreduce任务中Shuffle和排序的过程 流程分析: Map端: 1.每个输入分片会让一个map任务来处理,默认情况下,以HDFS的一个块的大小(默认为64M)为一个分片,当然我们也可以设置 ...

  3. android js调试

    http://blog.allenm.me/ 其他平台去这篇文章看 //js调试调试功能支持4.4版本以上的 if(Build.VERSION.SDK_INT >= Build.VERSION_ ...

  4. 证据对抗、证据链标准 z

    证据链乃诉讼中应对证据冲突的法宝   大部分当事人知道“证据链”这一诉讼专用术语,但几乎所有当事人将“证据链”理解成了人证.书证.物证,鉴定结论等证据的组合,将“证据链”视为不同种类证据依次出击的“组 ...

  5. 使用Async和Await进行异步编程(C#版 适用于VS2015) z

    你可以使用异步编程来避免你的应用程序的性能瓶颈并且加强总体的响应.然而,用传统的技术来写异步应用是复杂的,同时编写,调试和维护都很困难. VS2012介绍了简单的方法,那就是异步编程,它在.Net F ...

  6. SQL Server 2005 不允许远程连接解决方法

    刚刚安装的数据库系统,按照默认安装的话,很可能在进行远程连接时报错,通常是错误:“在连接到 SQL Server 2005 时,在默认的设 置下 SQL Server 不允许进行远程连接可能会导致此失 ...

  7. WCF和WebService中获取当前请求报文的方法

    WCF中: 1. 在hosting WCF的web.config中加入: <system.serviceModel> <serviceHostingEnvironment aspNe ...

  8. [ActionScript 3.0] AS3 绘制12面体

    package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; im ...

  9. java异常分类(运行时异常,可检查异常)

    NullPointerException:是运行时异常(RuntimeException),也叫非检查异常 所以我们抛出该类异常实例时,方法声明处无需添加throws来列举该类异常的抛出,编译器在编译 ...

  10. Java UDP网络编程 - 最简单示例

    UDP也是网络通讯中的一个重要协议,与TCP区别可参见浅谈TCP/IP 和 UDP的区别,本文就对Java UDP通讯做一个简单例子介绍 服务端: package wyf; import java.i ...