LeetCode 707. Design Linked List (设计链表)
题目标签:Linked List
题目让我们自己设计一个 linked list,可以是单向和双向的。这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code。
Java Solution:
Runtime: 56 ms, faster than 21.21%
Memory Usage: 45.2 MB, less than 88.89%
完成日期:07/08/2019
关键点:edge cases
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class MyLinkedList {
ListNode dummyHead;
/** Initialize your data structure here. */
public MyLinkedList() {
dummyHead = new ListNode(0);
dummyHead.next = null;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if(index < 0)
return -1;
ListNode cursor = dummyHead.next;
int i = 0;
while(cursor != null) {
if(i == index)
return cursor.val;
i++;
cursor = cursor.next;
}
return -1;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
public void addAtHead(int val) {
// case 1: no first node
if(dummyHead.next == null) {
ListNode node = new ListNode(val);
dummyHead.next = node;
node.next = null;
}
else {
ListNode node = new ListNode(val);
node.next = dummyHead.next;
dummyHead.next = node;
}
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
ListNode cursor = dummyHead;
// find the node's next is null, insert node after that node
while(cursor.next != null) {
cursor = cursor.next;
}
ListNode node = new ListNode(val);
node.next = cursor.next;
cursor.next = node;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
if(index < 0) {
addAtHead(val);
return;
}
ListNode cursor = dummyHead;
ListNode prevNode = dummyHead;
int i = -1;
while(cursor != null) {
// if find the spot to insert node
if(i + 1 == index) {
ListNode node = new ListNode(val);
node.next = cursor.next;
cursor.next = node;
return;
}
i++;
cursor = cursor.next;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if(index < 0)
return;
ListNode cursor = dummyHead.next;
ListNode prevNode = dummyHead;
int i = 0;
while(cursor != null) {
if(i == index) { // find the node to delete
prevNode.next = cursor.next;
cursor.next = null;
return;
}
i++;
prevNode = cursor;
cursor = cursor.next;
}
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 707. Design Linked List (设计链表)的更多相关文章
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- Leetcode707.Design Linked List设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- [LeetCode] Design Linked List 设计链表
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- #Leetcode# 707. Design Linked List
https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- [LeetCode] 622.Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- 【Leetcode_easy】707. Design Linked List
problem 707. Design Linked List 参考 1. Leetcode_easy_707. Design Linked List; 完
- 707. Design Linked List
1. 原始题目 Design your implementation of the linked list. You can choose to use the singly linked list ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 【LeetCode 23】合并K个排序链表
题目链接 [题解] 会归并排序吧? 就把这K个链表当成是K个数字就好. 然后做归并排序. 因为归并排序的时候本来就会有这么一个过程. [l..mid]和[mid+1..r]这两段区间都是有序的了已经. ...
- Vlan的相关知识点收纳
Q.思科Vlan的上限个数是多少? VLAN的范围:根据平台和软件版本不同,Cisco交换机最多支持4096个VLAN.VLAN号共有4096个,0-4095 0,4095:这两个号保留,仅限系 ...
- BZOJ 3252: 攻略(思路题)
传送门 解题思路 比较好想的一道思路题,结果有个地方没开\(long\) \(long\) \(wa\)了三次..其实就是模仿一下树链剖分,重新定义重儿子,一个点的重儿子为所有儿子中到叶节点权值最大的 ...
- TeleportArea可瞬移的目标位置
TeleportArea.png 1.可以自定义mesh ,想要什么区域就可以设置什么区域的mesh网格.2.Collider 组件必须有,否则无法进行位置移动,它用来检测激光笔是否打到该区域.3.M ...
- Ubuntu下设置静态网址
百度上找的图形界面下设置方式: 因为我这里的ubuntu版本是14.10版本 所以我先点击[系统设置],它位置在桌面左侧的菜单栏后面位置. 在系统设置页面,找到[硬件]选项里面的[网络]一项 然后再使 ...
- 带头结点的循环单链表----------C语言
/***************************************************** Author:Simon_Kly Version:0.1 Date: 20170520 D ...
- arttemplate 后台返回的数据格式问题
1. 2.JSON.parse() 方法用于将一个 JSON 字符串转换为对象. $("body").on("click","#analyze&quo ...
- 自旋锁spinlock
1 在单处理器上的实现 单核系统上,不存在严格的并发,因此对资源的共享主要是多个任务分时运行造成的. 只要在某一时段,停止任务切换,并且关中断(对于用户态应用程序,不大可能与中断处理程序抢临界区资源) ...
- pandas读取文件出现路径不存在的问题
我写的路径是绝对路径,其他的文件读写完全是没有问题的但是pandas就是不行,于是我改写为全路径:'E:/Python/KNN/iris.csv' 即可解决
- 前端(五)—— a、img、list标签
a标签.img标签.list标签 一.a标签 1.常用用法 <a href="https://www.baidu.com">前往百度</a> <a h ...