- 浏览: 18987 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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.
解题思路
本题要求一次遍历,想到的方法是引入一个列表保存每个节点。然后对列表中每个节点进行操作以完成对链表的修改。需要注意的是本题有很多错误校验。
相关知识点
(1)列表中删除元素
自己的代码
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.
解题思路
本题要求一次遍历,想到的方法是引入一个列表保存每个节点。然后对列表中每个节点进行操作以完成对链表的修改。需要注意的是本题有很多错误校验。
相关知识点
(1)列表中删除元素
list.remove(index);//根据下标删除元素
自己的代码
package leetcode; import java.util.ArrayList; import java.util.List; public class RemoveNthNodeFromEndOfList { public ListNode removeNthFromEnd(ListNode head, int n) { List<ListNode> list = new ArrayList<ListNode>(); //处理空链表 处理空操作 if(head == null) return null; if(n <= 0) return null; while(head != null){ list.add(head); head = head.next; } //处理异常操作 if(n > list.size()) return null; //如果只有一个元素 if(list.size() == 1) return null; //如果删除的是尾指针 if(n == 1){ list.get(list.size()-2).next = null; return list.get(0); } //如果删除的是指针头 if(list.size() == n) { list.remove(0); return list.get(0); } int index = list.size() - 1 - n; list.get(index).next = list.get(index + 2); list.remove(index + 1); return list.get(0); } public static void main(String[] args) { ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(2); ListNode node3 = new ListNode(3); ListNode node4 = new ListNode(4); ListNode node5 = new ListNode(5); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; //int n = 2; //int n = 5; int n = 1; RemoveNthNodeFromEndOfList rnnfeol = new RemoveNthNodeFromEndOfList(); //rnnfeol.removeNthFromEnd(node1, n).print(); //node1.print(); //System.out.println(rnnfeol.removeNthFromEnd(null, n)); //System.out.println(rnnfeol.removeNthFromEnd(node1, 0)); //System.out.println(rnnfeol.removeNthFromEnd(node1, -1)); //System.out.println(rnnfeol.removeNthFromEnd(node1, 6)); //System.out.println(rnnfeol.removeNthFromEnd(node5, 1)); rnnfeol.removeNthFromEnd(node4, 1).print(); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 396题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 282题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 417题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 300题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 326题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 345题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 352题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 321题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 343题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 354题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 305题目描述 Given a binary tree, find ... -
Path Sum
2014-10-20 15:37 291题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 230题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 289题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 254题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 316题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 333题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 396题目描述 Given two sorted integer a ...
相关推荐
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) 规则 常见的 分而治之 常见的