0019_Remove Nth Node From End of List
来源:互联网 发布:猪哼少 知乎 编辑:程序博客网 时间:2024/05/21 14:53
Given a linked list, remove the nth node from the end of list and return its head.
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.
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) { if(head == null && n == 0){ return head; } ListNode delNext = head; ListNode tail= head; for(int i = 0;i < n; ++i){ tail = tail.next; } while(tail != null && tail.next != null){ delNext = delNext.next; tail = tail.next; } if(delNext == head && tail == null){ head = head.next; }else{ delNext.next = delNext.next.next; } return head; }}
阅读全文
0 0
- 0019_Remove Nth Node From End of List
- leetcode 第一刷_Remove Nth Node From End of List
- LeetCode 19_Remove Nth Node From End of List
- 19_Remove Nth Node From End of List徒手尝试#2(Done)
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Python日期和时间
- C++模板类实现顺序表
- 一步一步完成SSM框架整合(Spring+Spring MVC + Mybatis)
- 取石子题目详解
- LeetCode 51. N-Queens 回溯
- 0019_Remove Nth Node From End of List
- 论ServletRequest.getRealPath()
- (单片机)显示温湿度程序
- C语言链表
- matlab获取文件夹中所有图像的方法
- Git之管理修改
- Linux下MongoDB安装和配置详解
- Dajngo24-单元测试
- dao模式