基本思路

  1. 定义两个指示指针a b
  2. 让a先行移动n+1个位置
  3. 若a指向了NULL的位置,则删除的是头节点(由于走过了n+1个节点刚好指在尾部的NULL上)
  4. 否则让b与a一起移动直至a->next,即a的下一个节点为NULL,则此时b的下一个节点为要删除的节点
  5. 删除下一个节点

代码实现

/**
* 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* a = head;
ListNode *b =head;
int c = 1;
//令a指向第n+1个节点
while(c!=n+1){
a = a->next;
c++;
}
if(a == NULL){
ListNode * t = head;
head = head->next;
delete t;
return head;
} //a b 同时向前走
while(a->next != NULL){
a = a->next;
b = b->next;
} ListNode * t = b->next;
b->next = b->next->next;
delete t;
return head;
}
};

LeetCode 删除链表倒数第N个节点的更多相关文章

  1. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

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

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

  3. 删除链表倒数第n个节点

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

  4. 19。删除链表倒数第N个节点

    class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next# 这道题还是很简单的,我们只 ...

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

  6. [LeetCode] 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. 删除单链表倒数第n个节点

    基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...

  8. leetcode 19.删除链表的第n个节点

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

  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. angularjs如何默认选中radio

    (1). 使用 ng-checked 即可.   <label class="radio-inline"> <input name="display&q ...

  2. react里面stateless函数的默认参数

    function fn({  children,  params,  dispatch,  location}) { }

  3. linux c 遍历目录及文件

    #include <dirent.h>void recovery_backend() { DIR * pdir ; struct dirent * pdirent; struct stat ...

  4. HQL(Hibernate Query Language)

    1. NativeSQL > HQL > EJB QL(JP QL 1.0) > QBC(Query By Criteria) > QBE(Query By Example)2 ...

  5. day006-多线程

    1. 线程概念 a)     什么是主线程 Java程序启动过程中自动创建的并执行main方法的线程称为主线程 主线程的执行路径: 从main方法开始到main方法结束 b)什么是子线程 除了主线程的 ...

  6. HCNA调整RIP的运行版本

    1.拓扑图 2.实验配置 R1配置RIPv1 md5加密认证 Please press enter to start cmd line! ############################### ...

  7. CSU计算机研究生推免

    考研复习 一开始,我是没有想到能够拿到研究生推免资格的,从今年3月份到整个暑假过完,一共6个月的时间,我一直在准备考研. 具体来说,我是在准备考研数学,整整6个月时间的数学复习,给我一种感觉,把大一大 ...

  8. 【[NOI2006]最大获利】

    题目 并不知到为什么这道题讲了这么久 我们发现这道题就是最小割的板子啊,完全可以套上文理分科的板子 把每个机器和\(T\)连边,容量为\(p_i\),这些\(p_i\)并不计入总贡献 对于每一个要求我 ...

  9. 2018.10.6 Hibernate配置文件详解-------ORM元数据配置 &&& hibernate主配置文件

    ORM既然是实体与关系数据库的映射,那就需要建立实体和关系数据库之间的基础数据,也可以称为元数据.简单的说就是表示类与表.列与属性(get.set方法)等等之间对应关系的数据. Customer.hb ...

  10. 2017.9.11 初入HTML学习

          第二章 静态网页开发技术 静态网页是指可以由浏览器解释执行而生成的网页,HTML是一组标签,负责网页的基本表现形式: JavaScript是在客户端浏览器运行的语言,负责在客户端与用户的互 ...