Leetcode 19 Remove Nth Node From End of List 链表
删除从后往前数的第n个节点
我的做法是将两个指针first,second
先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next->next为空
最后只要删除second->next
/**
* 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) {
if(!head) return head; ListNode* first = head;
for(int i = ; i < n - ; first = first->next, ++i); if(!first->next){
first = head;
head = head->next;
delete first;
return head;
} ListNode* second= head;
for(;first->next->next; first = first->next, second = second->next); ListNode* next = second->next;
second->next = next->next;
delete next; return head;
}
};
Leetcode 19 Remove Nth Node From End of List 链表的更多相关文章
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- [leetcode 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- Java [leetcode 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 ...
- Leetcode 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 19. Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [leetcode]19. 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: Given l ...
- 蜗牛慢慢爬 LeetCode 19. Remove Nth Node From End of List [Difficulty: Medium]
题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...
- [LeetCode] 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]19. 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: Given l ...
随机推荐
- [html]兼容 IE6 IE7 的简单网页框架
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- stream_copy_to_stream的使用
stream_copy_to_stream - 在数据流之间进行复制操作 例子: <?php //读写方式 $stream = fopen('php://temp', 'w+'); //如果成功 ...
- MyScript 手写识别数学公式、图形 自动计算
项目的地址 http://git.oschina.net/bimingcong/MyScript #说明:MyScript是一种能够自动识别用户在屏幕上的手势,然后转化为相应的数学公式.图形(比如三 ...
- 安装 composer SSL operation failed with code 1
gavin@webdev:~> curl -sS https://getcomposer.org/installer | php Downloading... Download failed: ...
- poi管道流的导入导出
/** * 导入学生信息 * * @param classid * @param uploadFilePath * @return */ public boolean uploadStudentFil ...
- Hibernate使用count(*)取得表中记录总数
/** * @TODO:查询某一年度的所有计划数量 */ public int findCountByYear(String currYear) { String hqlString = " ...
- [VBS]脚本中的字典、动态数组、队列和堆栈
今天用VBS脚本写了几个程序,用到了字典(Dictionary).动态数组(ArrayList).队列(Queue)和堆栈(Stack).现在写篇Blog总结一下 :-) 1.编写环境 今天突发奇想下 ...
- BufferedOutputStream的学习
今天写了一下一段代码,结果打开文件却发现要写入文件的内容不仅没写入,原来的内容也消失了,而控制台却显示原文件的内容都被读取出来了,代码如下: FileInputStream fileInputStre ...
- python基础语法(1)
一.基本概念 1. python中数有四种类型:整数.长整数.浮点数和复数. 整数, 如 1 长整数 是比较大的整数 浮点数 如 1.23.3E-2 复数 如 1 + 2j. 1.1 + 2.2j 2 ...
- 多线程JAVA篇(一)
解析AsyncTask源码之前,首先讲述与之相关的Java线程知识: 知识点清单 1.Thread类 2.Runnable接口 3.Callable接口 4.synchronized关键字 5.vol ...