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.
基础题,考验对链表的理解。
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.
基础题,考验对链表的理解。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode ln = null; int sum = 0; ListNode temp = head; while (temp != null){ sum++; temp = temp.next; } if(sum == 0 || n>sum || n<=0){ return head; } ln = head; int count = sum - n; if(count >0){ while(--count > 0){ ln = ln.next; if(ln == null){ return head; } } ln.next = ln.next.next; }else{ head = head.next; } return head; } }
发表评论
-
Merge k Sorted Lists
2015-03-12 19:55 325Merge k sorted linked lists and ... -
Generate Parentheses
2015-03-12 19:50 363Given n pairs of parentheses, w ... -
Generate Parentheses
2015-03-05 22:39 0Given n pairs of parentheses, w ... -
Valid Parentheses
2015-03-05 22:33 307Given a string containing just ... -
Letter Combinations of a Phone Number
2015-03-05 22:30 328Letter Combinations of a Phone ... -
4Sum
2015-03-05 22:26 310Given an array S of n integers, ... -
3Sum Closest
2015-03-05 22:25 289Given an array S of n integers, ... -
3Sum
2015-03-03 22:34 317Given an array S of n integers, ... -
Longest Common Prefix
2015-03-03 22:21 326Write a function to find the lo ... -
Roman to Integer
2015-03-03 22:20 326Given a roman numeral, convert ... -
Integer to Roman
2015-03-01 23:35 288Given an integer, convert it to ... -
Container With Most Water
2015-03-01 22:55 321Given n non-negative integers a ... -
Regular Expression Matching
2015-03-01 20:19 365Implement regular expression ma ... -
Palindrome Number
2015-02-13 22:08 329Determine whether an integer is ... -
String to Integer (atoi)
2015-02-13 11:07 340Implement atoi to convert a str ... -
Reverse Integer
2015-02-12 23:39 226Reverse digits of an integer. ... -
ZigZag Conversion
2015-02-12 23:37 253The string "PAYPALISHIRING ... -
Longest Palindromic Substring
2015-02-12 22:50 332Given a string S, find the long ... -
Add Two Numbers
2015-02-12 22:12 299You are given two linked lists ... -
Longest Substring Without Repeating Characters
2015-02-11 21:14 424[size=24px;]Longest Substring W ...
相关推荐
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) 规则 常见的 分而治之 常见的