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的更多相关文章

  1. 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 ...

  2. 【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 ...

  3. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【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 ...

  6. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  7. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  8. 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 + ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. ORA-28009: 应当以 SYSDBA 身份或 SYSOPER 身份建立 SYS 连接

    用 SQL*Plus 连接数据库的时候,除了用户名和密码外,还要在口令后面加一个主机字符串.如下: 请输入用户名:sys 口令:ANKoracle123,orcl as sysdba

  2. PIE SDK同态滤波

    1.算法功能简介 同态滤波是减少低频增加高频,从而减少光照变化并锐化边缘或细节的图像滤波方法. 同态滤波的流程为:空间域图像→对数运算→傅里叶正变换→同态滤波――傅里叶逆变换→指数运算→同态滤波结果. ...

  3. Mercedes BENZ C5 SD Connect Xentry Tab Kit Technical Support

    Why MB Star Diagnostic tool is so well-received by thousands of users, its technology and quality is ...

  4. ansible 实战项目之文件操作(二)

    一,前言 如果没有安装好的话看我以前的贴子哦!! 上次安装已经确定通了,所以下面步骤应该是完全ok的 特点: (1).轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可: (2 ...

  5. eclipse中注释快捷键

    手动注释: ①类注释:Shift+Alt+J ②方法注释:在方法上方输入/** 后点击回车 自动注释:点击菜单栏上的Window -->Preferences-->Java-->Co ...

  6. flask综合整理2

    session功能 首先我们知道session可以理解是一把钥匙,它存在在服务器上,其实在flask中也有session 1.系统自带的session from flask import sessio ...

  7. 把数据库内容显示在listview上

    数据库操作很简单,但用户想看见的是数据库里的内容,那么让数据库内容显示在屏幕上呢,下面做个简单演示,百变不离其中,先看步骤: 把数据库的数据显示至屏幕1. 任意插入一些数据 定义Javabean:Pe ...

  8. ICONIX方法(用例分析方法实例教程)

  9. PLC总结

    PLC编程总结 PLC控制部分总体有三大部分组成,PLC硬件,组态以及梯形图程序.PLC硬件应与组态一一对应,不容有任何偏差:而梯形图与操作的组态的IO口也应该一一对应.因此,整个系统达到了由梯形图程 ...

  10. webpack+babel+react操作小结

    最近学习了一下Webpack,个人感觉还是非常实用的,现在总结一下自己的学习笔记. 什么是 Webpack Webpack 是一个模块打包器.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定 ...