leetcode 19. Remove Nth Node From End of List (Linked list)
来源:互联网 发布:java项目学生管理系统 编辑:程序博客网 时间:2024/05/29 09:01
题意:
给你一个链表,让你删除倒数第n个数据。时间复杂度为O(n);
思路:
建立一个快指针和一个慢指针,快指针先向前n次,慢指针不动,之后一起向前,当快指针移动到末端,慢指针所处的位置就是要删除的数据的位置。
注意处理删除最后一个数据和第一个数据的特殊情况。
java代码:
class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode fast = head; ListNode slow = head; if(n==0){ return head; } if(n==1&&head.next==null){ return head.next; } while(n>0){ if(fast.next==null){ return head.next; } fast = fast.next; n--; } while(fast.next!=null){ slow = slow.next; fast = fast.next; } if(slow.next!=null) slow.next = slow.next.next; return head; }}
阅读全文
0 0
- LeetCode[Linked List]: Remove Nth Node From End of List
- 19. Remove Nth Node From End of List(Linked List)
- 19. Remove Nth Node From End of List leetcode list
- leetcode题解-328. Odd Even Linked List && 19. Remove Nth Node From End of List
- leetcode 19. Remove Nth Node From End of List (Linked 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)
- RXjava 使用
- FLYPRO飞豹荣膺第五届中国财经峰会双重大奖
- 前华为终端中国区总裁加盟一家C轮智能硬件公司,深圳基因到底强在哪里?
- TCP的三次握手(建立连接)和四次挥手(关闭连接)
- Spring Boot上传图片
- leetcode 19. Remove Nth Node From End of List (Linked list)
- 英伟达最新公布的眼球追踪技术,或将改变VR行业
- Java中的Atomic包使用指南
- 数据结构---C语言栈的链式表示与实现
- python中递归的深度,999
- 服务器通过vmware命令批量启动多个VMware虚拟机
- Codeforces Round #442 (Div. 2) A. Alex and broken contest
- 深度学习在搜索的应用:学术前沿与工业方案解析
- Android NDK系列(六)- 文件拆分与合并