【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 ...
随机推荐
- 【Python】python3实现网页爬虫下载图片
import re import urllib.request # ------ 获取网页源代码的方法 --- def getHtml(url): page = urllib.request.urlo ...
- python爬虫数据-下载图片经典案例
'''Urllib 模块提供了读取web页面数据的接口,我们可以像读取本地文件一样读取www和ftp上的数据.首先,我们定义了一个getHtml()函数: urllib.urlopen()方法用于打开 ...
- Linux C语言连接 sqlserver数据库
记录一下Linux下使用C语言连接sqlserver的方法. 连接前需要安装freetds. 参考: http://www.cnblogs.com/helloworldtoyou/p/6910075. ...
- e786. 创建JSpinner组件
This example demonstrates how to build three kinds of spinners. A number spinner: // Create a number ...
- C#自定义事件 范例:监视变量变化
很多时候我们需要程序具有一种功能,就是当满足某一条件时触发某个动作,使用C#的事件机制就可以达到这个目的下面的例子是一个很好的演示,这段代码实现了对一个变量的监视,一旦变量发生改变,就触发动作 定义事 ...
- 有空研究一下 superwebsocket (底层是 supersocket) 用来实现 web聊天什么的
参考:http://superwebsocket.codeplex.com/ 一个老外写的 asp.net 下socket的多钟方案推荐 :http://www.codeproject.com/Art ...
- table中td的内容换行。
table设置样式: table-layout: fixed; td设置: word-wrap: break-word;
- iPhone 配置使用工具
“iPhone 配置实用工具”可让您轻松地创建.维护和安装配置描述文件及对配置描述文件进行加密,跟踪和安装预置描述文件与授权的应用程序,以及采集包括控制台日志在内的设备信息. http://suppo ...
- Vi编辑器修改文件.bash_profile可解决backspace出现乱码问题,rlwrap 的安装。
Vi编辑器修改文件.bash_profile可解决backspace出现乱码问题 使用SecureCRT或是pietty_ch连接到一台安装有Oracle DB 10g的RHEL4.2的机器,linu ...
- 【中文分词】DAG、DP、HMM、Viterbi
http://blog.sina.com.cn/s/blog_8267db980102wq41.html http://www.cnblogs.com/leeshine/p/5804679.html ...