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. Feel Good(两遍单调栈维护区间+前缀和)

    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...

  2. vbscript 中对excel常见操作

    vbs 对excel的操作 删除.修改单元格,设置字体.背景色dim oExcel,oWb,oSheet Set oExcel= CreateObject("Excel.Applicatio ...

  3. 组合数取模介绍----Lucas定理介绍

    转载https://www.cnblogs.com/fzl194/p/9095177.html 组合数取模方法总结(Lucas定理介绍) 1.当n,m都很小的时候可以利用杨辉三角直接求. C(n,m) ...

  4. 【研究】Joomla二阶注入

    受影响Joomla版本:3.7.0 到 3.8.3 1.下载安装Joomla3.8.3,登录后台管理系统:http://127.0.0.1/joomla/administrator/index.php ...

  5. aoj0121

    一.题意:类似于华容道,输入是8个数字,输入虽然是一行,但实际是以两行的方式操作的.0表示空位,别的相邻数字可移动到该位置上.求最少移动步骤得到指定的状态. 二.思路:这题可以用BFS来解决.因为在每 ...

  6. mysql 5.7.20 server status 是stopped的解决办法

    mysql 5.7.20 server status 是stopped的解决办法 在安装mysql 5.7.20的过程中,前几个过程都没什么问题,但是最后一个步骤就出问题了.当check一直提示con ...

  7. Java调度线程池ScheduleExecutorService

    如果在一个ScheduleExecutorService中提交一个任务,这个任务的调度周期设置 的时间比任务本身执行的时间短的话会出现什么情况?也就是在线程调度时间已经到了 但是上次的任务还没有做完的 ...

  8. mysql 研发规范

    1. 命名    a) 有意义.    b) 数据库.表,都用小写,仅使用下划线和小写字母.    c) 索引以idx_开头.    d) 命名不要过长,尽量少于25个字符.    e) 不要使用保留 ...

  9. (转)[Nginx] – 配置文件优化 [一 ,二]

    [Nginx] – 安全优化 – 配置文件优化 [二] 原文:https://www.abcdocker.com/abcdocker/586 [Nginx] – 性能优化 – 配置文件优化 [一] 原 ...

  10. cloudemanager安装时出现ProtocolError: <ProtocolError for 127.0.0.1/RPC2: 401 Unauthorized>问题解决方法(图文详解)

    不多说,直接上干货! 问题详情 查看日志/var/log/cloudera-scm-agent/,得知 解决办法 $> ps -ef | grep supervisord $> kill ...