【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 解题思路:将数字 ...
随机推荐
- 45.Android 第三方开源库收集整理(转)
原文地址:http://blog.csdn.net/caoyouxing/article/details/42418591 Android开源库 自己一直很喜欢Android开发,就如博客签名一样, ...
- 【bzoj1562】 NOI2009—变换序列
http://www.lydsy.com/JudgeOnline/problem.php?id=1562 (题目链接) 题意 给出一个序列(0~n-1),这个序列经过某个变换会成为另外一个序列,但是其 ...
- java订电影票系统
public class Test { public static void main(String[] args) { BookTicket bookTicket = new BookTicket( ...
- CF Gym 100685A Ariel
传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- ci连贯操作的limit两个参数和sql是相反的
ci连贯操作的limit两个参数和sql是相反的 db->where("name =",'mary')->ge() name后面要有个空格否则报错当为一个字段 -> ...
- AndroidStudio-引用jar包及so文件
一.引用jar文件 1.将jar文件复制.粘贴到app的libs目录中: 2.右键点击jar文件,并点击弹出菜单中的"Add As Library",将jar文件作为类 ...
- 关于设置SQLPLUS提示符样式的方法----登陆配置文件,动态加载提示符
工作中用到 sqlplus mdsoss/mdsoss, 所以来了解一下sqlplus (C shell .cshrc文件里中alisa) 关于设置SQLPLUS提示符样式的方法 12638阅读 1评 ...
- C语言异常处理和连接数据库
#include <stdio.h> #include <setjmp.h> jmp_buf j; void Exception(void); double diva(doub ...
- NBUT1541 Rainwater 题解
http://cdn.ac.nbutoj.com/Problem/view.xhtml?id=1541 When rain, nocLyt discovered a magical phenomeno ...
- Chrome中的Device模块调式响应性设计
Chrome中的Device模块调式响应性设计 阅读目录 启用Device模块 Device模块设置介绍 自定义预设介绍 查看media queries 触发触摸事件 回到顶部 启用Device模块 ...