【LeetCode OJ】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:
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的更多相关文章
- 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 ...
- 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 ...
- 【LeetCode OJ】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length ...
- 【LeetCode OJ】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【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 ...
- 【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 ...
- 【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: ...
- 【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 ...
随机推荐
- Linux操作_磁盘管理_增加虚拟磁盘
环境:虚拟机 VM 12,Linux版本号 CentOS 7.3 1,在当前的虚拟机选项卡点击鼠标右键,选择“设置” 2,在弹出的对话框中左侧选中“磁盘”->点击下方“添加”按钮,在弹出的“添加 ...
- 使用@Ignore注解
断续上一节的例子,了解如何使用@Ignore注解.在测试类FirstDayAtSchoolTest中,我们将添加@Ignore注解到testAddPencils()方法.以这种方式,我们期望这个测试方 ...
- JUnit注解
在本节中,我们将提到支持在JUnit4基本注释,下表列出了这些注释的概括: 注解 描述 @Testpublic void method() 测试注释指示该公共无效方法它所附着可以作为一个测试用例. @ ...
- unity3d-----Collider 组件参考
Collider 组件参考 点击 属性检查器 下面的 添加组件 按钮,然后从 添加碰撞组件 中选择需要的 Collider 组件,即可添加 Collider组件到节点上. Collider 组件属性 ...
- Cisco交换机配置VLAN
Cisco IOS中有两种方式创建vlan,在全局模式下使用vlan vlanid命令,如switch(config)#vlan 10; 在vlan database 下创建vlan ,如 switc ...
- Windows 2008安装Oracle10g提示操作系统版本检查未通过
原文链接:http://www.cnblogs.com/emanlee/archive/2012/12/18/2824147.html 因开发环境需要,在Windows Server 2008 R2 ...
- R语言ggplot2-labs 函数
labs 函数主要有以下三个用途: 1) 设置图片的标题(title), 子标题(subtitle), 引用(caption) 代码示例: ggplot(mtcars, aes(mpg, wt, co ...
- vim介绍/vim颜色显示和移动光标/ vim一般模式下移动光标/ vim一般模式下复制、剪切和粘贴
5.1 vim介绍 5.2 vim颜色显示和移动光标 5.3 vim一般模式下移动光标 5.4 vim一般模式下复制.剪切和粘贴 vim 是vi的升级版本 vim 带有颜色显示 安装vim : y ...
- Thinkphp5 iis环境下安装报错400 500
要求一:服务器需要开启伪静态功能 要求二:新建文件夹web.config 放到入口目录下(如public/web.config 或者/web.config),内容如: <?xml versio ...
- webGL 光照
1.着色(shading) 在三维图形学术语“着色”的真正含义就是,根据光照条件重建“物体各表面明暗不一的效果”的过程.明白着色过程,需要考虑两件事: 1.发出光线的光源类型. 2.物体表 ...