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 ...
随机推荐
- jQuery中的事件和动画——《锋利的jQuery》(第2版)读书笔记2
第4章 jQuery中的事件和动画 jQuery中的事件 加载DOM $(document).ready(function(){ // 编写代码... }); 可以简写成: $(function( ...
- Java 容器:Collection 初探之 List
1 ///: JavaBasic//com.cnblogs.pattywgm.day1//CollectionTest.java 2 3 package com.cnblogs.pattywgm.da ...
- 设置阿里云maven中央仓库的settings.xml
本来想找一个可用的设置文件,结果乱七八糟的,干脆自己做了一个,同时还放上了Spring的SNAPSHOT和MILESTONE/RELEASE仓库,希望能帮到一些人. <?xml version= ...
- 在html里添加视频的方法
在html里添加本地视频的方法: <!DOCTYPE HTML><html><body><video width="320" height ...
- C# winform 代码生成
http://www.cnblogs.com/luomingui/archive/2012/09/02/2667217.html 双鱼林: http://www.crsky.com/soft/4941 ...
- (Python)元祖、字典
本节将学习元组.字典,以及其他涉及到的相关知识 1.元组 (tuple) 元组由圆括号括起来,元素之间用逗号相隔.元组是不可变对象,一旦定义了,就不能更改 >>> t=('a','b ...
- c语言中动态数组的建立
一维动态数组的创建,这个比较简单,直接上代码 #define _CRT_SECURE_NO_DEPRECATE #include<stdio.h> #include<stdlib.h ...
- linux环境变量查看及修改
例如用命令 echo $PATH 则可以查看该环境变量为/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 添加环境定义一个变量 ...
- php 和mysql httpd 简单网页的搭建
使用两台服务器 Centos 7 做 php和httpd和php-sql 服务的安装 CentOS 5 做php 和 mysql 的安装 1.搭建本地yum源 两台服务器 都是 2.关闭防火墙和s ...
- (最长公共子序列+推导)Love Calculator (lightOJ 1013)
http://www.lightoj.com/volume_showproblem.php?problem=1013 Yes, you are developing a 'Love calcula ...