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,
- 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.
简单的指针操作。
- /**
- * 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) {
- ListNode *pn=head;
- int sum=;
- while(pn->next){
- pn=pn->next;
- sum++;
- }
- int tn=sum-n+;
- if(tn==){
- return head->next;
- }
- pn=head;
- int i=;
- while(i!=tn-){
- pn=pn->next;
- i++;
- }
- pn->next = pn->next->next;
- return head;
- }
- };
leetcode 19. Remove Nth Node From End of List(链表)的更多相关文章
- Leetcode 19 Remove Nth Node From End of List 链表
删除从后往前数的第n个节点 我的做法是将两个指针first,second 先将first指向第n-1个,然后让first和second一起指向他们的next,直到first->next-> ...
- [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 ...
随机推荐
- sql数据分页
方法一.直接限制返回区间 (只适应于mysql) SELECT * FROM table WHERE 查询条件 ORDER BY 排序条件 LIMIT ((页码-1)*页大小),页大小; 优点:写法简 ...
- MySQL数据库 常用命令
1.MySQL常用命令 create database name;创建数据库 use databasename;选择数据库 drop database name 直接删除数据库,不提醒 show ta ...
- 基于Repository模式设计项目架构—你可以参考的项目架构设计
关于Repository模式,直接百度查就可以了,其来源是<企业应用架构模式>.我们新建一个Infrastructure文件夹,这里就是基础设施部分,EF Core的上下文类以及Repos ...
- HDMI原理图信号PIN脚
HDMI(19Pin)/DVI(16 pin)的功能是热插拔检测(HPD),这个信号将作为主机系统是否对HDMI/DVI是否发送TMDS信号的依据.HPD是从显示器输出送往计算机主机的一个检测信号.热 ...
- 基于友善之臂ARM-tiny4412--uboot源代码分析
/* * armboot - Startup Code for OMAP3530/ARM Cortex CPU-core * * Copyright (c) 2004 Texas Instrument ...
- Android中BaseAdapter使用基础点
Android中要填充一些控件(如ListView)经常须要用到Adapter来实现,经常使用的有ArrayAdapter,SimpleAdapter, CursorAdapter,BaseAdapt ...
- Spark源码分析之八:Task运行(二)
在<Spark源码分析之七:Task运行(一)>一文中,我们详细叙述了Task运行的整体流程,最终Task被传输到Executor上,启动一个对应的TaskRunner线程,并且在线程池中 ...
- Maven中央仓库地址(实用版)
最近做项目的时候,一直发现常用的oschina maven源一直都没有反应,后面发现原来oschina竟然关闭了maven源服务,后面经同事推荐了阿里云的maven源,这速度杠杠的 Maven 中央仓 ...
- 【ASP.NET】巧用Cookie实战
上篇介绍了究竟什么是Cookie.究竟是干什么用的,这篇博客具体具体的说一下.Cookie究竟怎样用. 首先建立如图所看到的的界面.通过该界面可登录到某个站点.详细要求例如以下: ·在首次登录后,将登 ...
- zoj 2068 - Chopsticks
题目:非常多人在一起吃饭.有两组单支的筷子,定义badness为一对筷子长度差的平方,求最小的badness和. 分析:dp,最大公共子序列类似物. 这里利用数学关系找到一个结论: a < b ...