【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 解题思路:将数字 ...
随机推荐
- 【转】getHibernateTemplate出现的所有find方法的总结
一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...
- str内部方法
代码 #str内部功能 name=' aK am\til.L iu' age=18 num=-11 ab='#' ac=('1','2','3','4','5','6','7') print(dir( ...
- Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server
目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...
- easyui之datagrid的使用
http://www.cnblogs.com/ruanmou001/p/3840954.html 一.神马是easyui jQuery EasyUI是一组基于jQuery的UI插件集合,而jQuery ...
- jquery------捕获异常处理
web.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC ...
- 锋利的jQuery-7--$.extend()
$.extend()主要有两个功能: 1.扩展jQuery对象 jQuery.extend({ min: function(a, b) { return a < b ? a : b; }, }) ...
- gcc/g++ 参数
-static 此选项将禁止使用动态库,所以,编译出来的东西,一般都很大,也不需要什么 动态连接库,就可以运行. -share 此选项将尽量使用动态库,所以生成文件比较小,但是需要系统由动态库.
- php 非缓冲查询
最近在开发一个PHP程序时遇到了下面的错误: PHP Fatal error: Allowed memory size of 268 435 456 bytes exhausted 错误信息显示允许的 ...
- jquery autocomplete 简单实用例子
<link href="../../themes/default/css/jquery.ui.all.css" rel="stylesheet" type ...
- c#接口和抽象类对比学习
什么是接口? 接口就是一种规范,协议(*),约定好遵守某种规范就可以写通用的代码. 定义了一组具有各种功能的方法.接口描述的是一种能力,具有这种能力的事物可以没任何关系.比如: public inte ...