题目标签: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. 关于if else 和 三目运算符的效率问题-java

    1.从类型转换上看,因为三目运算符在做判断的时候需要考虑到类型转换的问题,而if else 不需要考虑类型转换. 所以 if else 效率高一点. 2.从总体上看 A:需要考虑到循环自身所占用的时间 ...

  2. 【Flutter学习】基本组件之容器组件Container

    一,前言 Flutter控件本身通常由许多小型.单用途的控件组成,结合起来产生强大的效果,例如,Container是一种常用的控件,由负责布局.绘画.定位和大小调整的几个控件组成,具体来说,Conta ...

  3. SELinux导致PHP连接MySQL异常Can't connect to MySQL server的解决方法

    原文摘自:http://www.jb51.net/article/52581.htm 这篇文章主要介绍了SELinux导致PHP连接MySQL异常Can't connect to MySQL serv ...

  4. CSS:CSS 图像透明/不透明

    ylbtech-CSS:CSS 图像透明/不透明 1.返回顶部 1. CSS 图像透明/不透明 使用CSS很容易创建透明的图像. 注意:CSS Opacity属性是W3C的CSS3建议的一部分. 更多 ...

  5. HA配置

    主T800 eth0 192.168.2.32备T600 eth1 192.168.2.33安装nginx yum install -y nginx 关闭主备的防火墙iptables.selinux ...

  6. Hibernate 和 JPA 注解

    转载请注明:Hibernate 和 JPA 注解 | 言曌博客 1.@Entity(name="EntityName") 必须, name为可选,对应数据库中一的个表 2.@Tab ...

  7. HDU 6695 Welcome Party (贪心)

    2019 杭电多校 10 1005 题目链接:HDU 6695 比赛链接:2019 Multi-University Training Contest 10 Problem Description T ...

  8. CTF杂项思路工具分享————2019/5/30

    分享碰到的一些奇奇怪怪的杂项解题方式: 键盘坐标密码: 题目给出一段字符串:11 21 31 18 27 33 34 对照上面的表格,就可以很清晰的看出来密文为:QAZIJCV 猪圈码: 题目为: 一 ...

  9. 【WLAN常用语】—VAP

    文章摘自:https://forum.huawei.com/enterprise/zh/forum.php?mod=viewthread&tid=396533&page=1#pid22 ...

  10. Apache POI环境设置

    本章将指导完成Apache POI在Windows和Linux系统为基础的设置过程. Apache POI可以轻松地安装和集成,下面没有任何复杂的设置过程,通过几个简单步骤,目前Java环境,用户管理 ...