Given a linked list, remove the nth node from the end of list and return its head.

For example,

  1. Given linked list: 1->2->3->4->5, and n = 2.
  2.  
  3. 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,方便删除的操作。

代码:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode *removeNthFromEnd(ListNode *head, int n) {
  12. ListNode *target = head;
  13. ListNode *target_pre = NULL;
  14. ListNode *findthelast = head;
  15.  
  16. if (head == NULL)
  17. return head;
  18.  
  19. while(--n>)
  20. findthelast = findthelast->next;
  21.  
  22. while (findthelast->next) {
  23. target_pre = target;
  24. target = target->next;
  25. findthelast = findthelast->next;
  26. }
  27.  
  28. if (target_pre == NULL) {
  29. head = target->next;
  30. return head;
  31. }
  32.  
  33. target_pre->next = target->next;
  34.  
  35. return head;
  36. }
  37. };

文艺智慧青年解法:

(需要更多理解)

  1. class Solution
  2. {
  3. public:
  4. ListNode* removeNthFromEnd(ListNode* head, int n)
  5. {
  6. ListNode** t1 = &head, *t2 = head;
  7. for(int i = ; i < n; ++i)
  8. {
  9. t2 = t2->next;
  10. }
  11. while(t2->next != NULL)
  12. {
  13. t1 = &((*t1)->next);
  14. t2 = t2->next;
  15. }
  16. *t1 = (*t1)->next;
  17. return head;
  18. }
  19. };

附录:

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. LOJ6519. 魔力环(莫比乌斯反演+生成函数)

    题目链接 https://loj.ac/problem/6519 题解 这里给出的解法基于莫比乌斯反演.可以用群论计数的相关方法代替莫比乌斯反演,但两种方法的核心部分是一样的. 环计数的常见套路就是将 ...

  2. gym 100589A queries on the Tree 树状数组 + 分块

    题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...

  3. vue+webpack新项目总结1

    头部组件的  标题  根据不同的页面显示不同的标题 第一步: 在store 里面初始化全局变量 // vuex 通过状态管理数据 import Vue from 'vue' import Vuex f ...

  4. docker 镜像编译

    docker为我们提供了,包含源码的镜像, 可以要从docker hub上下载镜像来编译docker源码. 1.  docker pull docker-dev:v1.2.0,其他版本就到docker ...

  5. python小商店

    (1) 输入自己所有的钱.(2) 展示商品的序号,名称及其价格.(3) 输入要买商品的序号.(4) 输入要买商品的数量.(5) 购物车中显示购买的水果名称及其对应的数量和剩余钱.(6) 如果序号输入有 ...

  6. 解决windows10活动历史记录删除问题

    Windows10日常办公过程中系统会记录很多活动历史记录信息,我是不希望我的活动历史记录随时可以被别人查看,虽然电脑有设置密码,但是办公电脑还是习惯将历史记录删除掉,并且永远禁用windows10的 ...

  7. nfs 问题总结

    1. [root@backup read]# touch r01.txt   touch: cannot touch `r01.txt': Stale file handle   使用共享目录创建文件 ...

  8. Python Fileinput 模块

    作者博文地址:http://www.cnblogs.com/liu-shuai/ fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行. [默 ...

  9. TOJ 3651 确定比赛名次

    描述 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能直接获得每个队的比赛成绩 ...

  10. 【转】Python中不尽如人意的断言Assertion

    原文地址:Python中不尽如人意的断言Assertion Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛 ...