leetcode个人题解——#19 Remove Nth Node From End of List
思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作。
当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分配的内存空间,要加一个条件判断。
/**
* 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) {
if(head->next == NULL && n == ) return NULL;
if(n == ) return head;
ListNode* p = head;
ListNode* q = head;
for (int i = ; i < n; i++)
p = p->next;
int sum = ;
while(p != NULL && p->next != NULL)
{
p = p->next;
q = q->next;
sum++;
}
if(sum == && p == NULL) return q->next;
q->next = q->next->next;
return head;
delete p;
delete q;
delete head;
}
};
leetcode个人题解——#19 Remove Nth Node From End of List的更多相关文章
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- LeetCode题解(19)--Remove Nth Node From End of List
https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the ...
- 【leetcode❤python】 19. Remove Nth Node From End of List
#-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- 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 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
随机推荐
- webstorm如何支持markdown
首先安装markdown 插件 第一个即可.
- Zabbix——解决中文显示乱码
前提条件: 准备好上传工具,我用的是WinSCP 使用字体是微软雅黑,如果使用其他的更改名称即可. 更改Zabbix服务器的默认参数: vi /usr/share/zabbix/include/def ...
- 利用MyFlash闪回丢失数据(续)
last night,i've tested flashback by MyFlash tool,but failed,now let's do some other test with ...
- pt-online-schema-change在线修改表结构
工具简介 pt-osc模仿MySQL内部的改表方式进行改表,但整个改表过程是通过对原始表的拷贝来完成的,即在改表过程中原始表不会被锁定,并不影响对该表的读写操作.首先,osc创建与原始表相同的不包含数 ...
- 常用模块 - shutil模块
一.简介 shutil – Utility functions for copying and archiving files and directory trees.(用于复制和存档文件和目录树的实 ...
- Linux基础命令之文件和目录操作(一)
pwd print working directory的缩写,作用是显示当前工作目录的绝对路径,一般进行频繁切换路径时使用. -L 显示逻辑路径(或略软链接文件),不常用. -P 显示物理路径,不常用 ...
- 在线预览word,excel文档
Google Doc 示例:https://jsfiddle.net/7xr419yb/ Microsoft Office 示例:https://jsfiddle.net/gcuzq343/
- ie 8在打印网页的时候打印预览是空白的
win 7专业版系统中的ie 8在打印网页的时候打印预览是空白的,打印出来也是空白的,但是用别的浏览器打印没有问题 根据您的描述,该问题主要是由于保护模式下%Temp%\Low不正常工作引起的. 建议 ...
- 大数据学习--day11(抽象类、接口、equals、compareTo)
抽象类.接口.equals.compareTo 什么是抽象方法 ? 区分于正常的方法 1.使用了 abstract 修饰符 该修饰符修饰方法 则该方法就是抽象方 ...
- 关于python的菜鸟教程
1.正则表达式 http://www.runoob.com/regexp/regexp-metachar.html 2.python正则表达死 http://www.runoob.com/python ...