思路:设置两个指针,其中第二个指针比第一个延迟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的更多相关文章

  1. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

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

  3. 【leetcode❤python】 19. Remove Nth Node From End of List

    #-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...

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

  5. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  6. 【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 ...

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

  8. 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 移除链表倒数第N个节点

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

随机推荐

  1. 选择排序_c++

    选择排序_c++ GitHub 文解 选择排序的核心思想是对于 N 个元素进行排序时,对其进行 K = (N - 1) 次排序,每次排序从后(N + 1 - K)个数值中选择最小的元素与以 (K - ...

  2. CoacoaPods安装使与使用超级详细教程

    对于一个iOS开发的初学者来说,并不知道第三方类库的存在,知道了也不知道如何使用,那么下面便来介绍一下使用方法. iOS开发常用的第三方类库是GitHub:https://github.com/ 在上 ...

  3. UIPanGestureRecognizer 拖动TableView改变其高度

    需求:项目中要求tableView的高度随着手拖动的位置而改变如下图: 关键代码如下: - (void)viewDidLoad{ panGestureRecognizer = [[UIPanGestu ...

  4. 如何理解Hibernate的持久化?

    学习Hibernate,必须要理解什么是持久化?结合了一下网上的各位大佬的观点和自己的理解: 持久化概念 持久化是将程序数据在持久状态和瞬时状态间转换的机制.通俗的讲,就是瞬时数据(比如内存中的数据, ...

  5. 前端String转json

    1.data = eval("("+data+")");2.JSON.parse(data);

  6. Java使用zxing生成解读QRcode二维码

    1.maven的pom配置jar包,如果不实用maven请手动下载jar包 <dependency> <groupId>com.google.zxing</groupId ...

  7. Java设计模式(23)——行为模式之访问者模式(Visitor)

    一.概述 概念 作用于某个对象群中各个对象的操作.它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作. 引入 试想这样一个场景,在一个Collection中放入了一大堆的各种对象的引用 ...

  8. JSON解析工具——fastjson的简单使用

    从官方文档入手: 常见问题与快速上手:https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98 各种使 ...

  9. Solr第二讲——SolrJ客户端的使用与案例

    一.Solrj的使用 1.什么是Solrj solrj是访问Solr服务的java客户端(就像通过jedis操作redis一样),提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过Sol ...

  10. Caliburn.Micro 杰的入门教程5,Window Manager 窗口管理器

    Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...