https://leetcode.com/problems/remove-nth-node-from-end-of-list/

使用双指针法,可以仅遍历一次完成节点的定位

/**
* 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) {
ListNode* l = new ListNode();
l = head;
ListNode* r = new ListNode();
r = l;
while(n--)
r = r->next;
//用于判断head是否为单节点链表
if(r == NULL)
return head->next;
while(r != NULL && r->next != NULL)
{
r = r->next;
l = l->next;
}
ListNode* temp = new ListNode();
temp = l->next;
l->next = l->next->next;
if(temp)
delete(temp);
return head;
}
};

19. Remove Nth Node From End of List C++删除链表的倒数第N个节点的更多相关文章

  1. 19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点

    题目 Given a linked list, remove the n-th node from the end of list and return its head. *Example: Giv ...

  2. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

  3. LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)

    题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...

  4. 25.Remove Nth Node From End of List(删除链表的倒数第n个节点)

    Level:   Medium 题目描述: Given a linked list, remove the n-th node from the end of list and return its ...

  5. 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现

    题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二 ...

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

  7. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

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

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

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

随机推荐

  1. printf和std::cout ...endl

    printf效率要比std::cout...endl高些,可以减少打印所花时间

  2. js作用域题目

    window.number = 4var obj = { 'number': 4, 'tbl': (function(){ this.number *= 2; console.log(this.num ...

  3. Lintcode521-Remove Duplicate Numbers in Array-Easy

    Description Given an array of integers, remove the duplicate numbers in it. You should: Do it in pla ...

  4. python学习 day10打卡 函数的进阶

    本节主要内容: 1.函数参数--动态参数 2.名称空间,局部名称空间,全局名称空间,作用域,加载顺序. 3.函数的嵌套 4.gloabal,nonlocal关键字 一.函数参数--动态传参 形参的第三 ...

  5. template-web.js 引用变量、函数

    1.关键字   $imports.+变量/函数 {{if $imports.myParseInt(b.health_money)}} <span class="num"> ...

  6. Intellij idea创建maven项目并配置tomcat

    今天刷知乎的时候刷到这么一句话 我觉得还是蛮有趣的,形容的也比较到位,正好最近新建maven项目进行了thrift数据的传输,在此做一个记录 首先idea整合了maven,不需要单独下载 新建一个Pr ...

  7. live2d+cocos2dx示例工程

    环境 : win10 64bit visual studio 2013 cocos2d-x-3.9 Live2D_SDK_OpenGL_2.0.06_2_sample_3.3_en 首先安装visua ...

  8. C++.【转】C++数值类型与string的相互转换

    1.C++数值类型与string的相互转换 - JohnGu - 博客园.html(https://www.cnblogs.com/johngu/p/7878029.html) 2. 1.数值类型转换 ...

  9. Eclipse中出现无法找到Maven包Active Maven Profiles (comma separated)

    Eclipse中出现无法找到Maven包 2014年02月25日 16:51:30 阅读数:13057     症状:出现org.maven.ide.eclipse.MAVEN2_CLASSPATH_ ...

  10. sql server 我常用的语句

    1. computed column ) persisted; 2. unique nullable create unique nonclustered index[UniqueName] on [ ...