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 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.
分析:
题目中说n是合法的,就不用对n进行检查了。用标尺的思想,两个指针相距为n-1,后一个到表尾,则前一个到n了。
① 指针p、q指向链表头部;
② 移动q,使p和q差n-1;
③ 同时移动p和q,使q到表尾;
④ 删除p。
(p为second,q为first)
代码如下:
/**
* 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 == NULL || head->next == NULL) return NULL; ListNode * first = head;
ListNode * second = head; for(int i = 0;i < n;i++)
first = first->next; if(first == NULL)
return head->next; while(first->next != NULL){
first = first->next;
second = second->next;
} second->next = second->next->next;
return head;
}
};
Java:
public ListNode removeNthFromEnd(ListNode head, int n) { if (head == null || head.next == null) {
return null;
} ListNode first = head;
ListNode second = head; for (int i = 0; i < n; i++) {
first = first.next;
} if (first == null) {
return head.next;
} while (first.next != null) {
first = first.next;
second = second.next;
} second.next = second.next.next;
return head;
}
LeetCode 019 Remove Nth Node From End of List的更多相关文章
- 【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, Give ...
- [Leetcode][019] Remove Nth Node From End of List (Java)
题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointer ...
- 【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, Give ...
- [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】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】Remove Nth Node From End of List(easy)
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 ...
随机推荐
- Union-Find算法应用
上篇文章很多读者对于 Union-Find 算法的应用表示很感兴趣,这篇文章就拿几道 LeetCode 题目来讲讲这个算法的巧妙用法. 首先,复习一下,Union-Find 算法解决的是图的动态连通性 ...
- 4、Django之视图层
一 视图函数 视图函数,简称视图,属于Django的视图层,默认定义在views.py文件中,是用来处理web请求信息以及返回响应信息的函数,所以研究视图函数只需熟练掌握两个对象即可:请求对象(Htt ...
- python 与 百度人脸识别api
用python来做人脸识别代码量少 思路清晰, 在使用之前我们需要在我们的配置的编译器中通过pip install baidu-aip 即可 from aip import AipFac ...
- SpringCloud Alibaba+New搭建企业级开发框架(三):创建New工程
1.创建父工程:File > New > Project...,选择Maven,Create from archetype不要勾选,点击Next进入下一步,填写工程信息. image. ...
- go编译成exe后,打开出现闪退问题
今天博主编译了一个go小脚本,编译完成后用自己电脑试了一下没有问题 然而,当换了一台电脑后,一样是win7系统64位,出现了闪退 于是乎博主疯狂百度 看到网上说 入口文件 的顶部改成 packa ...
- 小程序后端获取openid (php实例)
小程序获取openid 首先,小程序授权登录的时候,前端就会获取到code 而后端接收到了code之后,就可以向微信发起请求,获取用户的openid代码如下: <?php $code = $_R ...
- webug第六关:这关需要rmb购买哦
第六关:这关需要rmb购买哦 首先登陆,tom 123456 进行抓包改包
- less和more的区别
more: 顾名思义显示更多less: 由于more不能后退,就取more的反义词less加上后退功能所以Linux里流传着这样一句话:"less is more". 总结下mor ...
- 又陷入知识盲区了,面试被问SpringBoot集成dubbo,我当时就懵了
前言 前两天在和粉丝聊天的时候,粉丝跟我说之前在面试的时候被问到SpringBoot这一块的知识被问的有点懵,和我问了不少这方面的东西.事后我想了想不如把这些东西分享出来吧,让更多的人看到,这样不管是 ...
- Docker这么火爆。章节一:带你详尽了解Docker容器的介绍及使用
前言 很多小伙伴可能在工作中都听说过Docker,但是实际工作中却没有使用过,听得多了,也对Docker内心有一种很深切的想了解,但是因为各种原因而不知道如何去了解而发愁,不要急,这篇文章带你认识Do ...