题目:Given a linked list, remove the nth node from the end of list and return its head.

For example:

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.
 struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {} };
ListNode *removeNthFromEnd(ListNode *head, int n)
{
ListNode *r = head, *s;
if (head == NULL)
return head;
int i = ;
for (ListNode *p = head; p != NULL; p = p->next)
{
i++;
}
if (i == n) //删除第一个节点
{
ListNode *l = head->next;
free(head);
return l;
}
for (int num = ; num < i - n - ; num++)//找到要删节点的前一个节点
{
r = r->next;
}
ListNode *tmp = r->next;
r->next = r->next->next;
free(tmp);
return head; }

【LeetCode OJ】Remove Nth Node From End of List的更多相关文章

  1. LeetCode OJ 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, Give ...

  2. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

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

  3. 【LeetCode OJ】Remove Element

    题目:Given an array and a value, remove all instances of that value in place and return the new length ...

  4. 【LeetCode OJ】Remove Duplicates from Sorted Array

    题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...

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

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

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

  7. 【leetcode】Remove Nth Node From End of List(easy)

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

  8. 【LeetCode每天一题】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:        ...

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

随机推荐

  1. ndarray的数据类型

    dtype参数 案例1: dtype(数据类型) 是一个特殊的对象,它含有ndarray , 将一块内存解释为特定数据类型所需的信息. 案例2:  利用astype 方法显式地转换其dtype 注意: ...

  2. 适合Eclipse Juno的UML插件

    今天给Eclipse安装UML插件,试了很多都不兼容,我用的Ecllipe版本是4.2 最后终于找到一个叫做AmatersUML的插件还能用,不过还是不够顺手,比如对泛型支持不够,不能从图形直接跳到相 ...

  3. Lambda动态排序

    private static IList<T> IListOrderBy<T>(IList<T> list, string propertyName) where ...

  4. lua----------------使用VS2015搭建lua开发环境的一些侥幸成功经验,

    所以本篇博文介绍在Windows平台下,使用VS2015搭建lua开发环境的一些侥幸成功经验,安装过程参考网上教程,安装过程如下(参考http://www.byjth.com/lua/33.html) ...

  5. c# mvc 获取 HtmlHelper 表达式值和时间格式化 去边框

    /// <summary> /// 返回没有边框的只读的TextBox标签 /// </summary> /// <typeparam name="TModel ...

  6. CSS3小清新下拉菜单 简易大方

    之前有分享过几款CSS3菜单和jQuery菜单,像这款HTML5/CSS3自定义下拉框 3D卡片折叠动画3D效果非常华丽,这次要分享的这款相对比较简单,很适合用在用户的操作面板上.先来看看效果图: 怎 ...

  7. 共享锁(S锁)和排它锁(X锁)

    释义 共享锁:(读取)操作创建的锁.其他用户可以并发读取数据,但任何事物都不能获取数据上的排它锁,直到已释放所有共享锁. 共享锁(S锁)又称为读锁,若事务T对数据对象A加上S锁,则事务T只能读A:其他 ...

  8. dubbox 的各种管理和监管[转]

    dubbo官方自带了dubbo-admin及dubbo-simple/dubbo-monitor-simple二个子项目用于服务治理及服务监控. 一.dubbo-admin的部署 这个比较简单,编译打 ...

  9. useradd groupadd passwd usermod userdel chfn id

    chfn   修改用户信息 id 显示当前用户和用户组的id

  10. js粘贴事件paste简单解析及遇到的坑

    在用户执行粘贴操作的时候,js能够获得剪切板的内容,本文讨论一下这个问题. 目前只有Chrome支持获取剪切板中的图片数据.还好需要这个功能的产品目前只支持Chrome和Safari,一些Chrome ...