[Linked List]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.
Note:
Given n will always be valid.
Try to do this in one pass.
/**
* 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) {
int len = ;
ListNode* p = head;
while(p){
len++;
p=p->next;
}
p = head;
ListNode* pre = NULL;
int m = len -n;
while(m){
pre = p;
p=p->next;
m--;
}
if(pre){
pre->next = p->next;
}else{
p = head;
head = head->next;
}
delete(p);
return head;
}
};
[Linked List]Remove Nth Node From End of List的更多相关文章
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- Merge Two Sorted Lists & Remove Nth Node From End of List
1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...
- leetcode-algorithms-19 Remove Nth Node From End of List
leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
随机推荐
- 关于VS 2010 RDLC 报表的详细使用说明
各位技术屌丝们好, 之前我用了很长一段时间通过不断的研究揣摩,终于把RDLC报表给搞透了,今天跟大家做个总结,希望能够帮助到大家. 需求分析 我想把datagridview 中的数据打印出来. 首先 ...
- 2014年1月9日 Oracle 内存与结构
Oracle启动时为启动一个实例 主要为 实例 SVG 数据库文件 其它文件 1.Oracle: 内存 进程 其他文件 1.1 SVG内存(Cache) 1.1.1 共享池(Shared Poo ...
- 查询DB中每个表占用的空间大小
使用如下sql script可以获得每个数据库表所占用的空间大小,单位是KB create table #Data(name varchar(100),row varchar(100),reserve ...
- linux中BASH_SOURCE[0]
在C/C++中,__FUNCTION__常量记录当前函数的名称.有时候,在日志输出的时候包含这些信息是非常有用的.而在Bash中,同样有这样一个常量FUNCNAME,但是有一点区别是,它是一个数组而非 ...
- Oracle学习之常见错误整理
一.ORA-12154: TNS: 无法解析指定的连接标识符 在程序中连接Oracle数据库的方式与其他常用数据库,如:MySql,Sql Server不同,这些数据库可以通过直接指定IP的方式连接, ...
- Eclipse代码风格设置
在编写代码的过程中,代码的呈现形式是通过eclipse的Formatter配置文件所控制的.我们可以按照自己的习惯生成属于自己的代码风格配置文件,方便规范以后的代码编写形式.具体的操作步骤如下所示:( ...
- Java数组的复制
初学Java的时候,需要复制数组的时候,一下子就想到使用赋值语句“=”,例如:array1 = array2:但后来慢慢发现,这个语句并不能将array2的内容复制给array1,而是将array2的 ...
- 去除除服串中的某些字符,不用String内置方法
import java.util.regex.Matcher; import java.util.regex.Pattern; public class test { public static vo ...
- AngularJS中serivce,factory,provider的区别
一.service引导 刚开始学习Angular的时候,经常被误解和被初学者问到的组件是 service(), factory(), 和 provide()这几个方法之间的差别.This is whe ...
- spring下配置dbcp,c3p0,proxool[转]
不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不但可以通过JNDI获取应用 ...