- 浏览: 171725 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
题目的要求是给定一个链表和一个整数n,从反方向数删除第n个节点。解决这道题目我们用双指针。根据题意我们分析,链表的头结点可能会被删除,因此我们要用一个辅助节点helper来记录链表的头结点。设定两个指针都指向helper,首先让fast指针移动n步,然后fast指针和slow指针同时移动,直到fast指针为空,此时slow的下一个节点恰好为从后面数第n个节点。代码如下:
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.
题目的要求是给定一个链表和一个整数n,从反方向数删除第n个节点。解决这道题目我们用双指针。根据题意我们分析,链表的头结点可能会被删除,因此我们要用一个辅助节点helper来记录链表的头结点。设定两个指针都指向helper,首先让fast指针移动n步,然后fast指针和slow指针同时移动,直到fast指针为空,此时slow的下一个节点恰好为从后面数第n个节点。代码如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode helper = new ListNode(0); helper.next = head; ListNode fast = helper; ListNode slow = helper; for(int i = 0; i < n; i++) fast = fast.next; while(fast.next != null) { fast = fast.next; slow = slow.next; } slow.next = slow.next.next; return helper.next; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 221Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 219You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 336Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 325Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 443Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 498Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 423Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 611Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 420The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 378Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 511Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 521Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 364All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 853Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 876Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 547Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 592Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 757Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 724You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 623For a undirected graph with tre ...
相关推荐
19.Remove_Nth_Node_From_End_of_List删除链表的倒数第N个节点【LeetCode单题讲解系列】
19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove ...
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 ...
Remove Nth Node From End of List LeetCode 42 Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode ...
Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked List 字符串 ...
java二叉树算法源码 ...Remove Nth Node From End of List Medium 21 合并两个有序链表 Merge Two Sorted Lists Easy 141 判断链表是是否存在环 Linked List Cycle Easy 142 环形链表II Linked List Cycle I
lru cache leetcode #算法解题报告 主要记录我每天做的题目,包括leetcode, 剑指offer等在线编程平台,以前做过的等时间够再一起...19.Remove Nth Node From End of List 20.Valid Parentheses 21.Merge Two Sorted L
扔鸡蛋 leetcode LeetCode-Note-Mary Mary's ...List(删除链表的倒数第N个节点) 153. Find Minimum in Rotated Sorted Array(寻找旋转排序数组中的最小值) 2020/12/09 300. Longest Increasing
leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行...Remove Nth Node From End of List JavaScript O(n) O(1) Medium 21 Merge Two Sorted Lists JavaScript O(n)
Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 18 3 sum 扩展版, 外层多套一个循环即可。注意判断重复及细节优化 19 细节:nodelist前插入一个...
leetcode 316 leetcode 题解更新脚本 用于快速的更新题解、同步...Remove Nth Node From End of List Easy -> Medium 33 Search in Rotated Sorted Array Hard -> Medium 35 Search Insert Position Medium -> Easy 36
Nth Node From End of List Merge Two Sorted Lists 两个链表的交集 Remove Duplicates from Sorted List Palindrome Linked List LL中的插入排序 使用额外的缓冲区从未排序的链表中删除重复项 细绳 确定字符串是否...
leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号...List删除链表的倒数第N个节点 Combination Sum组合总和 Combination Sum II组合总和 II Permutations全排列 Permutations II全排列 II Maximum Suba
Remove Nth Node From End of List 中等 链表 20 有效的字符串/有效的括号 21 合并两个有序链表 23 合并K个排序链表 24 两两交换链表中的节点 26 从排序数组中删除重复项 数组 27 移除元素 简单 数组 43 字符串相乘 ...
26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...
19.Remove Nth Node From End of List: 本题要求移除倒数第n个点,进阶解法要求只遍历一遍完成。 我采用字典,将节点编号和节点存进字典里,这样遍历一遍记得到了所有,然后移除即可。 看了答案的一遍过滤的方法是用...
Remove Nth Node From End of List Swap Nodes in Pairs Spiral Matrix Path Sum II Copy List with Random Pointer Building H2O Fizz Buzz Multithreaded hard Merge k Sorted Lists Reverse Nodes in k-Group ...
019_Remove_Nth_Node_From_End_of_List 020_Valid_Parentheses 021_Merge_Two_Sorted_Lists 022_Generate_Parentheses 023_Merge_k_Sorted_Lists 024_Swap_Nodes_in_Pairs 025_Reverse_Nodes_in_k-Group 026_...
2sum leetcode ...Remove_Nth_Node_From_End_of_List.cpp - invert_Binary_Tree.cpp - 对称树.cpp - BST_Or_Not.cpp - level_order_traversal.cpp - exponentiation_by_squaring.cpp - Maximum_Depth_B
lru缓存leetcode leetcode 我的 leetcode Javascript / Swift / Mysql 解决方案 典型问题 动态规划 子阵列 ..../topics/linked-list/remove-nth-node-from-end-of-list.md) 规则 常见的 分而治之 常见的