problem:

  1. Given a linked list, remove the nth node from the end of list and return its head.
  2.  
  3. For example,
  4.  
  5. Given linked list: 1->2->3->4->5, and n = 2.
  6.  
  7. After removing the second node from the end, the linked list becomes 1->2->3->5.
  8. Note:
  9. Given n will always be valid.
  10. Try to do this in one pass.

删除单链表的倒数第n个节点

thinking:

(1)这里的 head 是头指针。指向第一个结点!!

!别搞混了。

(2)为了避免反复计数,採用双指针,先让第一个指针走n-1步,再一起走,这样,等前面指针走到最后一个非空结点时。后面一个指针正好指向待删除结点的前驱!!!

(3)延伸:

头结点不是必须的,一般不用。经常使用的是用一个头指针head指向第一个元素结点!!

!。!这道题就是!!!!

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *removeNthFromEnd(ListNode *head, int n) {
  12. // Start typing your C/C++ solution below
  13. // DO NOT write int main() function
  14. if (head == NULL)
  15. return NULL;
  16.  
  17. ListNode *pPre = NULL;
  18. ListNode *p = head;
  19. ListNode *q = head;
  20. for(int i = 0; i < n - 1; i++)
  21. q = q->next;
  22.  
  23. while(q->next)
  24. {
  25. pPre = p;
  26. p = p->next;
  27. q = q->next;
  28. }
  29.  
  30. if (pPre == NULL)
  31. {
  32. head = p->next;
  33. delete p;
  34. }
  35. else
  36. {
  37. pPre->next = pPre->next->next;
  38. delete p;
  39. }
  40.  
  41. return head;
  42. }
  43. };

leetcode 题解 || Remove Nth Node From End of List 问题的更多相关文章

  1. [LeetCode 题解]: 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, Give ...

  2. LeetCode 019 Remove Nth Node From End of List

    题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...

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

  4. 【leetcode】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 ...

  5. 【leetcode】Remove Nth Node From End of List(easy)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

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

  7. 【JAVA、C++】LeetCode 019 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, Give ...

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

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

随机推荐

  1. 5款工具助你写出更好的Java代码

    1.FindBugs 顾名思义,FindBugs是一款帮助开发者发现bug的工具,它是一个开源项目,遵循GNU公共许可协议,运行的是Java字节码而不是源码. 它是一款静态分析工具,它检查类或者JAR ...

  2. JSP内置对象的cookie和session实现简单登录界面

    创建一个index.jsp页面 <%@ page language="java" contentType="text/html; charset=utf-8&quo ...

  3. Spark2.1.0之源码分析——事件总线

    阅读提示:阅读本文前,最好先阅读<Spark2.1.0之源码分析——事件总线>.<Spark2.1.0事件总线分析——ListenerBus的继承体系>及<Spark2. ...

  4. 【NOI2017】游戏

    好久没写 $2-sat$ 了……(话说这东西哪里还考过) 题意 有 $3$ 种赛车 $A,B,C$ 和 $4$ 种赛道 $a,b,c,x$,赛车 $A$ 不适用于赛道 $a$,赛车 $B$ 不适用于赛 ...

  5. 轻量级神经网络平台tiny-dnn实践

        tiny-dnn跑起来                    github:  https://github.com/tiny-dnn/tiny-dnn#build 先上github下载tin ...

  6. hdu 3613 KMP算法扩展

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. uva 11468 AC自动机+概率DP

    #include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...

  8. GridView主键列不让编辑时应该修改属性DataKeyNames

    原文发布时间为:2008-08-02 -- 来源于本人的百度文章 [由搬家工具导入] 为了防止GridView主键被编辑,应该在GridView属性DataKeyNames里面写上主键

  9. C#:使用WebRequest类请求数据

    本文翻译于:https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx 下列程序描述的步骤用于从服务器请求一个资源,例如,一个We ...

  10. 第6章 I/O多路复用

    前一章节客户端同时处理两个输入:标准输入和TCP套接字,然而问题在于客户端阻塞于fgets调用期,服务器进程被杀死后,服务器tcp虽然可以正确发送一个fin,但进程正阻塞于标准输入,它无法看到eof, ...