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 ...
随机推荐
- selenium - 弹出框操作
# 6. 弹出框操作 # 6.1 页面弹出框操作# 页面弹出框 是一个html页面的元素,由用户在页面的操作触发弹出# (1)执行触发操作之后,等待弹出框出现之后,# (2)再定位弹出框中的元素并操作 ...
- python之路 --- python文件模式
文件模式: 打开文件的模式有: r,只读模式(默认). w,只写模式.[不可读:不存在则创建:存在则删除内容:] a,追加模式.[可读: 不存在则创建:存在则只追加内容:] "+&quo ...
- python列出指定目录下的所有目录和文件
import os import docx def scanfile(rootdir): result = [] for f in os.walk(rootdir): for files in f[2 ...
- C#发送邮件异常,返回信息乱码
发邮件时出现了异常: 在 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 在 ...
- Docker Caffe部署
Caffe是一个清晰而高效的深度学习框架,纯粹的C++/CUDA架构,支持命令行.Python和MATLAB接口:可以在CPU和GPU直接无缝切换 Caffe的优势 上手快:模型与相应优化都是以文本形 ...
- 关于后台返回excel文件的问题
一般情况ajax请求只能获取解析非流文件类型,而excel是流文件类型,这个时候获取到的数据会是一串乱码的字符串 想要下载这个excel文件,通过form表单模拟的方式可以解决 var form = ...
- 批处理命令之Start的详细用法
Start启动单独的“命令提示符”窗口来运行指定程序或命令.如果在没有参数的情况下使用,start 将打开第二个命令提示符窗口. 语法start ["title"] [/dPath ...
- 慕课 python 操作数据库2 银行转账实例
CREATE TABLE `account` ( `acctid` ) DEFAULT NULL COMMENT '账户ID', `) DEFAULT NULL COMMENT '余额' ) ENGI ...
- 标准C程序设计七---65
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- 标准C程序设计七---33
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...