基本思路

  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. Xcode6中Swift没有智能提示和自动补全功能

    今天在学习Swift的过程中,编写代码,发现没有智能提示和自动补全功能,一阵不适应,在网上溜达了下,找到了解决办法,测试可行 中文系统新建工程,copyright里有日期,2014年,“年”..然后删 ...

  2. asp.net后台获取html控件的值

    1.asp.net后台获取前台type=text控件的值 前台:<input name="txtName" class="username" type=& ...

  3. 在 Eclipse Juno 上安装 Marketplace

    Select Help/Install new software... from the menu, select the Juno update site (http://download.ecli ...

  4. IntelliJ、ReSharper 6折 加入慧都“惊喜惠”

    慧都2013岁末回馈惊喜不断!著名的软件开发公司JetBrains旗下所有产品加入"惊喜惠"活动环节, JAVA IDE——IntelliJ IDEA,.NET效率工具集——ReS ...

  5. Android 解决Glide 加载图片缓慢(第一次加载不出来图片)的Bug

  6. 【Gradle】 Gradle 综合

    Gradle User Guide:http://www.gradle.org/docs/current/userguide/userguide.html 针对它的中文翻译:http://ask.an ...

  7. java web的安全约束--表单的验证

    例子,表单和JDBCRealm的安全验证 参考了一篇文章http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228698.html 1.要在wab ...

  8. SOL的补充

    之前写过一些关于远程安装系统的文档,但是对于SOL还是糊涂不清. Serial Console 可以将输入输出转发到串行接口(com1, com2), 假如你有串行读取设备,就可以看到显示,控制输入. ...

  9. windows下使用VNC进行远程连接

    在 windows 电脑上安装 VNC,包含 VNC server 和 VNC viewer,如果仅需要被操控或操控他人,选择型下载安装 VNC server 或 VNC viewer 即可. 在需要 ...

  10. 【Leetcode】【Easy】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, Give ...