【JAVA、C++】LeetCode 019 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.
解题思路一:
先计算length,然后删除
JAVA实现:
static public ListNode removeNthFromEnd(ListNode head, int n) {
if(n<=0)
return head;
ListNode ln=head;
int i=1;
while(ln.next!=null){
ln=ln.next;
i++;
}
if(i==n)
return head.next;
ln=head;
for(;i>n+1;i--)
ln=ln.next;
ln.next=ln.next.next;
return head;
}
解题思路二:
一个指针先走n步,另一个指针跟上。
C++:
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
if (n <= )
return head;
ListNode* cur = head;
for (int i = ; i < n-; i++) {
if (cur->next != NULL)
cur = cur->next;
else return head;
}
if (cur->next == NULL) {
ListNode*temp = head;
head = head->next;
delete temp;
return head;
}
cur = cur->next;
ListNode* cur2 = head;
while (cur->next != NULL) {
cur2 = cur2->next;
cur = cur->next;
}
cur = cur2->next;
cur2->next = cur->next;
delete cur;
return head;
}
};
【JAVA、C++】LeetCode 019 Remove Nth Node From End of List的更多相关文章
- LeetCode 019 Remove Nth Node From End of List
题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 018 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
随机推荐
- Shell good example
(1) Source code #! /bin/bash reference () { pa=\$"$1" echo $pa x=`eval &qu ...
- BZOJ 1066 POJ 2711 [SCOI2007]蜥蜴
与POJ 1815 Friendship类似,该题之前也做过 目前处于TLE状态.样例已经通过 1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec Memory Limit: ...
- Activator.CreateInstance 反射实例化对象
public class CommonReq { private String TransNo { get; set;} public String SubmitData { get; set; } ...
- ci创建zip
public function createZip() { $this->load->library("zip"); $name = "test.text&q ...
- Python 科学计算涉及模块
模块1.数据基础 numpy 模块2.数值运算 scipy 模块3.符号运算 sympy 模块4.图形绘制 matplotlib
- sencha touch list(列表)、 store(数据源)、model(模型)详解
//求职 Ext.define('app.model.Staff', { extend: 'Ext.data.Model', config: { fields: [{ name: 'id', type ...
- meclipse中project facet问题
meclipse中project facet问题 (2012-02-14 14:59:48) 转载▼ 标签: 杂谈 分类: 技术 一般出现在从别处import的项目上,只有项目文件夹上有红叉,其他地方 ...
- Mongodb c#增删改查
写在前面 最近项目需要,就研究了下mongodb,也是为了快速上手,就自己弄了一个简单的例子,这里记录一下. Mongodb 传统的关系数据库一般由数据库(database).表(table).记录( ...
- 12个常用的js正则表达式
在这篇文章里,我已经编写了12个超有用的正则表达式,这可是WEB开发人员的最爱哦. 1.在input框中只能输入金额,其实就是只能输入最多有两位小数的数字 //第一种在input输入框限制 <i ...
- 浏览器兼容性小整理和一些js小问题(后面会继续更新)
最近在啃jQuery的源码,估计会啃到很多浏览器兼容性的问题,所以整理一下 1,IE下的内存泄露. 在IE中不在DOM树中的独立节点有javascript变量引用它的时候不会被回收. 解决:手动将该j ...