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

代码如下:

 /**
* 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. sql异常

    表结构 Id int UncheckedTitle nvarchar(50) CheckedValue nvarchar(1000) CheckedRemark nvarchar(1000) Chec ...

  2. English Notes

    1. Thanks for stopping by.慢走不送. 2. I don't like to judge a book by it's cover.我不喜欢以貌取人. 3. Rush hour ...

  3. CentOS7 备忘录

    //安装CentOS7 1.用LinuxLive USB Creator,LiveUSB Creator设置U盘安装不成功,UltraISO可以. 2.旧电脑(x8le)安装时报"/dev/ ...

  4. protoc的protoc-gen-grpc-java插件

    编译 protoc-gen-grpc-java插件 的文档在: https://github.com/grpc/grpc-java/tree/master/compiler  编译的步骤: Chang ...

  5. python中报错总结

    python标准异常 异常名称 描述     BaseException 所有异常的基类 SystemExit 解释器请求退出 KeyboardInterrupt 用户中断执行(通常是输入^C) Ex ...

  6. List<Object>和List<String>

    下面的代码在JAVA中是不可以的: import java.util.*; public class Test { public static void main(String[] args) { L ...

  7. 程序员必备:Oracle日常维护命令

        上一篇讲了Linux的日常维护命令,这篇讲讲Oracle的日常维护命令.工作中需要使用Oracle数据库的童鞋们,相信或多或少都需要对Oracle做一些基本的维护操作,例如导入导出总该有吧?( ...

  8. 在 Visual Studio 2013 中使用 Grunt, Bower 和 NPM

    在 Visual Studio 2015 中提供了对于 Grunt 和 Gulp 的内置支持,在 Visual Studio 2013 中怎么办呢?微软将 2015 中的特性作为几个独立的扩展发布出来 ...

  9. linux多线程

    #include <pthread.h> pthread_t pid; pthread_create(&pid, 0, pFunc, pArgs); //创建线程(linux下线程 ...

  10. DFS

    HDU1181  http://acm.hdu.edu.cn/showproblem.php?pid=1181 #include<stdio.h> #include<algorith ...