LeetCode笔记:19. Remove Nth Node From End of List
来源:互联网 发布:网站权限数据库设计 编辑:程序博客网 时间:2024/05/21 10:52
问题:
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个节点并返回链表头。
例子,
给出链表: 1->2->3->4->5, 以及 n = 2.
在移除倒数第二个节点后,列表变为了 1->2->3->5。注意:
给出的n一定是有效的。
尝试在一轮循环中做。
思路:
题目的难点在于你不知道遍历到第几个节点时是要删除的倒数第n个节点。
我的做法很笨,遍历一遍记录所有节点的值和位置,然后重新根据值和位置创建新的链表,跳过要删除的那个位置的节点,因为此时知道总节点数了就可以推出是第几个节点了。在操作时要注意一些特殊情况,比如只有一个节点时、删除头结点时要怎么处理。
代码(Java):
/** * 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) { int[] nodeVal = new int[100]; int[] nodeIndex = new int[100]; nodeVal[0] = head.val; nodeIndex[0] = 0; int index = 1; while (head.next != null) { head = head.next; nodeVal[index] = head.val; nodeIndex[index] = index; index++; } ListNode newHead; int begin = 0; if (index == 1) return null; else if (index == n) { newHead = new ListNode(nodeVal[1]); begin = 1; } else newHead = new ListNode(nodeVal[0]); ListNode tempNode = newHead; for (int i = begin+1; i < index; i++) { if (i != index - n) { ListNode newNode = new ListNode(nodeVal[i]); tempNode.next = newNode; tempNode = newNode; } } return newHead; }}
他山之石:
public ListNode removeNthFromEnd(ListNode head, int n) { ListNode start = new ListNode(0); ListNode slow = start, fast = start; slow.next = head; //Move fast in front so that the gap between slow and fast becomes n for(int i=1; i<=n+1; i++) { fast = fast.next; } //Move fast to the end, maintaining the gap while(fast != null) { slow = slow.next; fast = fast.next; } //Skip the desired node slow.next = slow.next.next; return start.next;}
看一下这个巧妙的做法,他设了快慢两个标记,初始都在头结点,快的先往后遍历,遍历到与头结点相差为n的时候停止,然后快的和慢的一起往后走,直到快的走到了链表尾节点打止,这时候快慢两个节点间相差的节点数正好是n,也就是说慢的所在的下一个节点正好是要删除的节点,直接跳过去就可以了,一遍遍历完成,很棒。
合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_
- LeetCode笔记:19. Remove Nth Node From End of List
- leetcode笔记:Remove Nth Node From End of List
- leetcode笔记--Remove Nth Node From End of List
- 19. Remove Nth Node From End of List leetcode list
- [LeetCode]19.Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- 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
- [leetcode] 19.Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- leetcode 19. Remove Nth Node From End of List
- [LeetCode OJ] 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(JAVA)
- leetcode 19. Remove Nth Node From End of List
- 【震惊】15项世界互联网领先科技成果发布!特斯拉、华为等上榜...
- iOS .car 文件内容提取
- centos 7 vsftpd 安装
- iOS开发 ☞ UITabbar
- 使用GPU利用DL学梵高作画–实践
- LeetCode笔记:19. Remove Nth Node From End of List
- spring的两种定时任务配置方式
- struts2中用标签创建一个新的url
- 第一篇--安装编译环境
- linux内核模块怎么编译——Makefile解析
- 39级台阶问题
- iOS开发-低版本XCode如何支持高版本iOS系统进行真机调试
- 第二篇--下载Android源码
- codeforces 734F (数学)