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 ...
随机推荐
- 开源框架:DBUtils使用详解
一.先熟悉DBUtils的API: 简介:DbUtils是一个为简化JDBC操作的小类库. (一)整个dbutils总共才3个包: 1.包org.apache.commons.dbutils 接 ...
- Linux sed命令用法
概述 sed命令是一个面向字符流的非交互式编辑器,不允许用户与它进行交互操作.sed是以行为单位处理文本内容的.在shell中,可以批量修改文本内容. 用法 sed [选项] [动作] 选项与参数:- ...
- Eclipse报The default workspace'xxxxx' is in use or cannot be created Pl
原因:出现这种情况一般是workspace的配置文件中出现了.lock文件(workspace/.metadata/.lock),锁定了workspace.把.lock文件删除即可.如果该文件不能删除 ...
- Python模块、包、异常、文件(案例)
Python模块.包.异常.文件(案例) python.py #模块 # Python中的模块(Module),是一个Python文件,以.py文件结尾,包含了Python对象定义和Python语句, ...
- jQuery的简单函数
1. jQuery函数的基本语法: $(document).ready(function(){ //代码块: }) 2.window.onload()和$(document).ready()的区分: ...
- React的安装方法
一:直接使用 BootCDN 的 React CDN 库,地址如下: <script src="https://cdn.bootcss.com/react/16.4.0/umd/rea ...
- Currency Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 货币 ...
- vimrc 配置
" All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by" the call to : ...
- GeekOS: 一、构建基于Ubuntu9.04的实验环境
参考:http://www.cnblogs.com/wuchang/archive/2009/05/05/1450311.html 补充:在最后步骤中,执行bochs即可弹出运行窗口
- zabbix经常报警elasticsearch节点TCP连接数过高问题
单服务器最大tcp连接数及调优汇总 单机最大tcp连接数 网络编程 在tcp应用中,server事先在某个固定端口监听,client主动发起连接,经过三路握手后建立tcp连接.那么对单机,其最大并发t ...