【Leetcode】【Easy】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.
普通青年解法:
设置三个指针A\B\C,指针A和B间隔n-1个结点,B在前A在后,用指针A去遍历链表直到指向最后一个元素,则此时指针B指向的结点为要删除的结点,指针C指向指针B的pre,方便删除的操作。
代码:
/**
* 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 *target = head;
ListNode *target_pre = NULL;
ListNode *findthelast = head; if (head == NULL)
return head; while(--n>)
findthelast = findthelast->next; while (findthelast->next) {
target_pre = target;
target = target->next;
findthelast = findthelast->next;
} if (target_pre == NULL) {
head = target->next;
return head;
} target_pre->next = target->next; return head;
}
};
文艺智慧青年解法:
(需要更多理解)
class Solution
{
public:
ListNode* removeNthFromEnd(ListNode* head, int n)
{
ListNode** t1 = &head, *t2 = head;
for(int i = ; i < n; ++i)
{
t2 = t2->next;
}
while(t2->next != NULL)
{
t1 = &((*t1)->next);
t2 = t2->next;
}
*t1 = (*t1)->next;
return head;
}
};
附录:
C++二重指针深入
【Leetcode】【Easy】Remove Nth Node From End of List的更多相关文章
- LeetCode OJ 292.Nim Gam19. 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 (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses
1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...
- Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 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 ...
随机推荐
- plsql11破解注册码
plsql11.0.6.1796-64bit的可以用注册码: Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqz serial Number: passwo ...
- ORA-28009: 应当以 SYSDBA 身份或 SYSOPER 身份建立 SYS 连接
用 SQL*Plus 连接数据库的时候,除了用户名和密码外,还要在口令后面加一个主机字符串.如下: 请输入用户名:sys 口令:ANKoracle123,orcl as sysdba
- Tensorlflow-解决非线性回归问题
import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt #使用numpy生成200个随机点,范围从-0.5到0 ...
- HDU - 2276 位运算矩阵快速幂
挺有意思的一道题 要会运用一些常见的位运算操作进行优化 题目的本质就是要求下面的式子 \(dp[i][j+1]=(dp[i-1][j]+dp[i][j]) \mod 2\) (第\(i\)个字符在\( ...
- docker一键安装
1.任意新服务器上一键安装最新版docker curl -s https://get.docker.com/ | sh 注:安装完成之后,docker默认是没有启动的,需要启动docker 2.doc ...
- Linux设备驱动开发基础--内核定时器
1. Linux内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,其实现位于 <Linux/timer.h> 和 kernel/timer.c 文件 ...
- Oracle 数据库和Sql Server数据库的区别
Oracle数据库的访问方式,和SqlServer数据库是有很大差别的,下面用图来说明: 1.Sql Server数据库 SqlServer数据库的访问方式,大致是:假设用户通过sa登录SqlServ ...
- 基于setTimeout制作滚动广告板
很多网站在其门户页面的上方正中央都会放置一个滚动广告板,用于显示一些推荐信息,用户点击即可进入浏览.比较常见的就是各个公司的官网,电商网站的首页等. 下面是天猫的滚动广告板截图. 其实,不需要借助于什 ...
- 121、Django rest framework入门使用
框架介绍 为你的django平台通过model生成对应的restfull api,并可以通过对应的http接口来进行 post .get.put.delete等操作.本文是也并非入门级别,不会带你去了 ...
- Java学习第二十四天
1:多线程(理解) (1)JDK5以后的针对线程的锁定操作和释放操作 Lock锁 (2)死锁问题的描述和代码体现 (3)生产者和消费者多线程体现(线程间通信问题) 以学生作为资源来实现的 资源类:St ...