【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 ...
随机推荐
- LOJ6519. 魔力环(莫比乌斯反演+生成函数)
题目链接 https://loj.ac/problem/6519 题解 这里给出的解法基于莫比乌斯反演.可以用群论计数的相关方法代替莫比乌斯反演,但两种方法的核心部分是一样的. 环计数的常见套路就是将 ...
- gym 100589A queries on the Tree 树状数组 + 分块
题目传送门 题目大意: 给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重. 思路: 思考后发现,以dfs序建立树状数 ...
- vue+webpack新项目总结1
头部组件的 标题 根据不同的页面显示不同的标题 第一步: 在store 里面初始化全局变量 // vuex 通过状态管理数据 import Vue from 'vue' import Vuex f ...
- docker 镜像编译
docker为我们提供了,包含源码的镜像, 可以要从docker hub上下载镜像来编译docker源码. 1. docker pull docker-dev:v1.2.0,其他版本就到docker ...
- python小商店
(1) 输入自己所有的钱.(2) 展示商品的序号,名称及其价格.(3) 输入要买商品的序号.(4) 输入要买商品的数量.(5) 购物车中显示购买的水果名称及其对应的数量和剩余钱.(6) 如果序号输入有 ...
- 解决windows10活动历史记录删除问题
Windows10日常办公过程中系统会记录很多活动历史记录信息,我是不希望我的活动历史记录随时可以被别人查看,虽然电脑有设置密码,但是办公电脑还是习惯将历史记录删除掉,并且永远禁用windows10的 ...
- nfs 问题总结
1. [root@backup read]# touch r01.txt touch: cannot touch `r01.txt': Stale file handle 使用共享目录创建文件 ...
- Python Fileinput 模块
作者博文地址:http://www.cnblogs.com/liu-shuai/ fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行. [默 ...
- TOJ 3651 确定比赛名次
描述 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排 名,但现在裁判委员会不能直接获得每个队的比赛成绩 ...
- 【转】Python中不尽如人意的断言Assertion
原文地址:Python中不尽如人意的断言Assertion Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛 ...