【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 ...
随机推荐
- PIE SDK波段运算
1.算法功能简介 波段运算(Band Math)工具能够方便的执行图像中的各个波段的加减乘除.三角函数.指数.对数等数学函数计算,也可以使用IDL编写的函数. 由于每个用户都有独特的需求,利用此工具用 ...
- encoding specified in XML prolog (UTF-8) is different from that specified in page directive (utf-8)
myeclipse下启动项目后出现错误:encoding specified in XML prolog (UTF-8) is different from that specified in pag ...
- poi生成excel报表合并列
功能任务 poi导出excel统计报表,有合并列的. 根据结构生成层级. 目标 1输入一个linkshashmap LinkedHashMap<String, Object> fieldM ...
- (转) 来自: http://man.linuxde.net/tee
tee命令文件过滤分割与合并 tee命令用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin.简单的说就是把数据重定向到给定文件和屏幕上. 存在缓存机制,每1024个 ...
- Ace教你一步一步做Android新闻客户端(三) JSON数据解析
对于服务器端来说,返回给客户端的数据格式一般分为html.xml和json这三种格式,现在给大家讲解一下json这个知识点, 1 如何通过json-lib和gson这两个json解析库来对解析我们的j ...
- 03-struts2获得servetAPI
1 原理 三个域合一的时候相同的键值对以小的域为准.ActionContext 对象创建:每次请求的时候都会创建一个与请求对应的 ActionContext 对象.ActionContext 销毁:请 ...
- k8s单节点集群部署应用
之所以用k8s来部署应用,就是因为k8s可以灵活的控制集群规模,进行扩充或者收缩.生产上我们要配置的参数较多,命令行的方式显然不能满足需求,我们应该使用基于配置文件的方式.接下来做一个部署的demo: ...
- 【Linux】Linux下使用Docker快速部署Oracle数据库
安装最新Docker wget -qO- https://get.docker.com/ | sh` 安装 docker-compose sudo curl -L https://github.com ...
- springboot整合mongo多数据源
该实例已测试 POM.XML <!-- Spring Boot mongodb 依赖--> <dependency> <groupId>org.springfram ...
- MySQL中设置同一张表中一个字段的值等于另一个字段的值
今天遇到了一个需求,我在一张表中新增了一个字段,因为这张表以前已经有很多数据了,这样对于以前的数据来说,新增的这个字段的值也就是为该字段的默认值,现在需要将新增的这个字段添加上数据,数据来源为同表的另 ...