leetcode题解(十):19. Remove Nth Node From End of List
2019-04-01 13:39
501 查看
题意:移除链表的倒数第n个节点
例子:
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.
public ListNode removeNthFromEnd(ListNode head, int n) { //依然是两根指针的思路 ListNode start = new ListNode(0); ListNode slow = start, fast = start; slow.next = head; //快指针先走n步 for(int i=1; i<=n+1; i++) { fast = fast.next; } //快慢指针一起走 while(fast != null) { slow = slow.next; fast = fast.next; } //慢指针所指的位置就是倒数第n个节点,删除即可 slow.next = slow.next.next; return start.next; }
相关文章推荐
- LeetCode 19之Remove Nth Node From End of List的Java题解(三种解法)
- Leetcode题解 19. Remove Nth Node From End of List
- leetcode题解-19. Remove Nth Node From End of List
- LeetCode题解(19)--Remove Nth Node From End of List
- LeetCodet题解--19. Remove Nth Node From End of List(删除链表的倒数第n个元素)
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
- leetcode题解-328. Odd Even Linked List && 19. Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List 解题报告
- leetcode 19. Remove Nth Node From End of List
- 【小熊刷题】Remove Nth Node From End of List <Leetcode 19, Java>
- LeetCode问题解答:19. Remove Nth Node From End of List
- Leetcode 19.Remove Nth Node From End of List
- [Leetcode19] Remove Nth Node From End of List
- leetcode 19. Remove Nth Node From End of List
- LeetCode——19. Remove Nth Node From End of List
- LeetCode 19 Remove Nth Node From End of List 移除倒数第N个节点
- [LeetCode 19] Remove Nth Node From End of List Solutions
- [leetcode] 19. Remove Nth Node From End of List
- LeetCode19:Remove Nth Node From End of List
- leetcode 19 Remove Nth Node From End of List