题目标签: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 (设计链表)的更多相关文章

  1. 【LeetCode】Design Linked List(设计链表)

    这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...

  2. Leetcode707.Design Linked List设计链表

    设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...

  3. [LeetCode] Design Linked List 设计链表

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

  4. #Leetcode# 707. Design Linked List

    https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...

  5. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  6. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  7. 【Leetcode_easy】707. Design Linked List

    problem 707. Design Linked List 参考 1. Leetcode_easy_707. Design Linked List; 完

  8. 707. Design Linked List

    1. 原始题目 Design your implementation of the linked list. You can choose to use the singly linked list ...

  9. 【LeetCode】707. Design Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 【算法】BitMap

    转自:https://www.seoxiehui.cn/article-45186-1.html 需求: 为满足用户标签的统计需求,小灰利用Mysql设计了如下的表结构,每一个维度的标签都对应着Mys ...

  2. Linux的命名空间

    1. 为什么提供命名空间 命名空间是一种轻量级的虚拟化手段. 传统的虚拟化软件,是虚拟化多个不同的操作系统,对共享资源的限制很大. 通过提供命名空间,可以让进程与进程之间,用户与用户之间彼此看不到对方 ...

  3. 牛客 最大值减去最小值小于或等于 num 的子数组数量

    题目链接:https://www.nowcoder.com/practice/5fe02eb175974e18b9a546812a17428e?tpId=101&tqId=33086& ...

  4. 转载:vue-cli 脚手架 安装

    声明:本文转载自https://www.cnblogs.com/loveyaxin/p/7094089.html 一. node安装 1)如果不确定自己是否安装了node,可以在命令行工具内执行: n ...

  5. 使用CSS将图片转换成黑白(灰色、置灰) & 毛玻璃效果

    法1⃣️: IE浏览器: filter: gray; 其他浏览器: .gray { -webkit-filter: grayscale(100%); -moz-filter: grayscale(10 ...

  6. MySQL数据库(一)—— 数据库介绍、MySQL安装、基础SQL语句

    数据库介绍.MySQL安装.基础SQL语句 一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 ...

  7. zip压缩详细分析

    该文章转自:http://www.cnblogs.com/esingchan/p/3958962.html (文章写得很详细,让我对zip压缩有了了解,感谢博主,贴在这是为了防止忘了有这么好的文章,侵 ...

  8. ubuntu安装WPS替代office

    安装 1.下载地址:http://community.wps.cn/download/(去WPS官网下载) 下载第一个即可 2.执行安装命令: sudo dpkg -i wps-office_10.1 ...

  9. Django学习铺垫

    Web框架本质 所有的web服务本质都是一个socket服务端,用户浏览器就是一个socket客户端,这样就实现了自己的web框架 ,但是自己的写的框架肯定很low,各种工能崩溃,所以我们就要学习py ...

  10. 微信小程序のwxs

    WXS(WeiXin Script)是小程序的一套脚本语言,结合 WXML,可以构建出页面的结构. wxs可以说就是为了满足能在页面中使用js存在的,在wxml页面中,只能在插值{{ }}中写简单的j ...