【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 ...
随机推荐
- 上下栏固定, 中间滚动的HTML模板
因为用position是脱离文档流的,所以在最上面嘛, 中间用overflow:auto就会出现滚动效果 代码 <!DOCTYPE html> <html lang="en ...
- 使用tomcat搭建centos的yum源
最近在折腾大数据,需要搭建一个yum源.一般的做法是在CentOS中安装httpd,然后将rpm包放入/var/www/html下面,再执行[createrepo .]即可. 不过虚拟机对传文件终归是 ...
- JUnit4 基本用法实例
本教程介绍了在JUnit4中支持的基本注解. import org.junit.*; import static org.junit.Assert.*; import java.util.*; /** ...
- e824. 获得和设置JSplitPane中的子组件
// Create a left-right split pane JSplitPane hpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, lef ...
- #HTTP协议学习# (八)状态码详解
可直接参考:http://www.cnblogs.com/TankXiao/archive/2013/01/08/2818542.html
- dubbo学习过程、使用经验分享及实现原理简单介绍
一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一些非重要模块上,慢慢引入到一些稍微重要的功能上,半年时间,学习过程及线上使用遇到的些问题在此总结下. 整理这篇文章差不 ...
- Java 利用POI操作PPT
解析PPT文件中的图片 import java.io.File; import java.io.FileOutputStream; import org.apache.poi.hslf.HSLFSli ...
- 决策树-Cart算法二
本文结构: CART算法有两步 回归树的生成 分类树的生成 剪枝 CART - Classification and Regression Trees 分类与回归树,是二叉树,可以用于分类,也可以用于 ...
- kafka学习之-server.properties详细说明
http://blog.csdn.net/lizhitao/article/details/25667831 -- 参考文章 http://kafka.apache.org/documentatio ...
- SQL2005数据库置疑处理
2005中遇到置疑.丢失日志时按照网上常见的MSSQL2000修复方法来做, 结果发现行不通,甚至连一步都做不下去.其实,在MSSQL2005在处理置疑问题的思 路与MSSQL2000是一致的,但具体 ...