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.
/** * 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) { if (head == null || n < 1) { return head; } ListNode cur = head; while (cur != null) { n--; cur = cur.next; } if (n == 0) { head = head.next; } while (n < 0) { cur = head; while (++n != 0) { cur = cur.next; } cur.next = cur.next.next; } return head; } }
相关推荐
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 ...
c c语言_leetcode 0019_remove_nth_node_from_end_of_list.zip
java入门 java_leetcode题解之019_Remove_Nth_Node_From_End_of_List
js js_leetcode题解之19-remove-nth-node-from-end-of-list.js
c语言入门 C语言_leetcode题解之19-remove-nth-node-from-end-of-list.c
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 ...
* 从列表末尾删除第N个节点(Remove Nth Node From End of List):从链表末尾删除第N个节点。 5. 栈和队列操作: * 有效的括号(Valid Parentheses):判断括号是否有效。 * 生成括号(Generate Parentheses):...
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 ...
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)
19. Remove Nth Node From End of List:移除链表的倒数第N个节点。这个问题可以通过双指针法来解决。 20. Valid Parentheses:判断给定的字符串是否是有效的括号字符串。 LeetCode的答案大全中整理的这些问题,...
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中的插入排序 使用额外的缓冲区从未排序的链表中删除重复项 细绳 确定字符串是否...
- **2.2.7 Remove Nth Node From End of List** - 删除链表倒数第n个节点。 - 实现思路:使用快慢指针,快指针先走n步,当快指针到达尾部时,慢指针指向待删除节点的前一个节点。 - **2.2.8 Swap Nodes in Pairs...
leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号...List删除链表的倒数第N个节点 Combination Sum组合总和 Combination Sum II组合总和 II Permutations全排列 Permutations II全排列 II Maximum Suba