leetcode 题解 || Remove Nth Node From End of List 问题
problem:
- Given a linked list, remove the nth node from the end of list and return its head.
- For example,
- Given linked list: 1->2->3->4->5, and n = 2.
- After removing the second node from the end, the linked list becomes 1->2->3->5.
- Note:
- Given n will always be valid.
- Try to do this in one pass.
删除单链表的倒数第n个节点
thinking:
(1)这里的 head 是头指针。指向第一个结点!!
!别搞混了。
(2)为了避免反复计数,採用双指针,先让第一个指针走n-1步,再一起走,这样,等前面指针走到最后一个非空结点时。后面一个指针正好指向待删除结点的前驱!!!
(3)延伸:
头结点不是必须的,一般不用。经常使用的是用一个头指针head指向第一个元素结点!!
!。!这道题就是!!!!
!
- /**
- * 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) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- if (head == NULL)
- return NULL;
- ListNode *pPre = NULL;
- ListNode *p = head;
- ListNode *q = head;
- for(int i = 0; i < n - 1; i++)
- q = q->next;
- while(q->next)
- {
- pPre = p;
- p = p->next;
- q = q->next;
- }
- if (pPre == NULL)
- {
- head = p->next;
- delete p;
- }
- else
- {
- pPre->next = pPre->next->next;
- delete p;
- }
- return head;
- }
- };
leetcode 题解 || Remove Nth Node From End of List 问题的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- 【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 ...
- 【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 ...
- [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, ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- 5款工具助你写出更好的Java代码
1.FindBugs 顾名思义,FindBugs是一款帮助开发者发现bug的工具,它是一个开源项目,遵循GNU公共许可协议,运行的是Java字节码而不是源码. 它是一款静态分析工具,它检查类或者JAR ...
- JSP内置对象的cookie和session实现简单登录界面
创建一个index.jsp页面 <%@ page language="java" contentType="text/html; charset=utf-8&quo ...
- Spark2.1.0之源码分析——事件总线
阅读提示:阅读本文前,最好先阅读<Spark2.1.0之源码分析——事件总线>.<Spark2.1.0事件总线分析——ListenerBus的继承体系>及<Spark2. ...
- 【NOI2017】游戏
好久没写 $2-sat$ 了……(话说这东西哪里还考过) 题意 有 $3$ 种赛车 $A,B,C$ 和 $4$ 种赛道 $a,b,c,x$,赛车 $A$ 不适用于赛道 $a$,赛车 $B$ 不适用于赛 ...
- 轻量级神经网络平台tiny-dnn实践
tiny-dnn跑起来 github: https://github.com/tiny-dnn/tiny-dnn#build 先上github下载tin ...
- hdu 3613 KMP算法扩展
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- uva 11468 AC自动机+概率DP
#include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...
- GridView主键列不让编辑时应该修改属性DataKeyNames
原文发布时间为:2008-08-02 -- 来源于本人的百度文章 [由搬家工具导入] 为了防止GridView主键被编辑,应该在GridView属性DataKeyNames里面写上主键
- C#:使用WebRequest类请求数据
本文翻译于:https://msdn.microsoft.com/en-us/library/456dfw4f(v=vs.110).aspx 下列程序描述的步骤用于从服务器请求一个资源,例如,一个We ...
- 第6章 I/O多路复用
前一章节客户端同时处理两个输入:标准输入和TCP套接字,然而问题在于客户端阻塞于fgets调用期,服务器进程被杀死后,服务器tcp虽然可以正确发送一个fin,但进程正阻塞于标准输入,它无法看到eof, ...