Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and nextval is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

  • get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  • addAtHead(val) : 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.
  • addAtTail(val) : Append a node of value val to the last element of the linked list.
  • addAtIndex(index, val) : 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. If index is negative, the node will be inserted at the head of the list.
  • deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
linkedList.get(1); // returns 2
linkedList.deleteAtIndex(1); // now the linked list is 1->3
linkedList.get(1);    // returns 3 注意一下corner case,一定先要问清楚corner case的处理。
 class MyLinkedList {
class Node {
int val;
Node next;
public Node(int val) {
this.val = val;
}
}
private Node head;
private int size; /** Initialize your data structure here. */
public MyLinkedList() { } /** 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 >= size || index < ) return -;
Node current = head;
for (int i = ; i < index; i++) {
current = current.next;
}
return current.val;
} /** 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) {
Node node = new Node(val);
node.next = head;
head = node;
size++;
} /** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
Node node = new Node(val);
size++;
if (head == null) {
head = node;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.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 > size) return;
if (index <= ) {
addAtHead(val);
} else {
size++;
Node current = head;
for (int i = ; i < index - ; i++) {
current = current.next;
}
Node node = new Node(val);
node.next = current.next;
current.next = node;
}
} /** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (index >= size || index < ) return;
size--;
if (index == ) {
head = head.next;
} else {
Node current = head;
for (int i = ; i < index - ; i++) {
current = current.next;
}
current.next = current.next.next;
}
}
}
												

Design Linked List的更多相关文章

  1. 【Leetcode_easy】707. Design Linked List

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

  2. [Swift]LeetCode707. 设计链表 | Design Linked List

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

  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. 707. Design Linked List

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

  5. #Leetcode# 707. Design Linked List

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

  6. LeetCode707:设计链表 Design Linked List

    爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...

  7. C#LeetCode刷题之#707-设计链表(Design Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...

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

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

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

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

随机推荐

  1. python--批量修改文件夹名

    python代码如下: import os , re import os.path rootdir = r'C:\Users\Administrator\Desktop\222' # rootdir ...

  2. 校验正确获取对象或者数组的属性方法(babel-plugin-idx/_.get)

    背景: 开发中经常遇到取值属性的时候,需要校验数值的有效性. 例如: 获取props对象里面的friends属性 props.user && props.user.friends &a ...

  3. 诊断和修复Web测试记录器(Web Test Recorder)问题

    http://www.cnblogs.com/oscarxie/articles/1045430.html Database LoadTest2010 script C:\Program Files ...

  4. 洛谷P4317 花神的数论题

    洛谷题目链接 数位$dp$ 我们对$n$进行二进制拆分,于是就阔以像十进制一样数位$dp$了,基本就是套模板.. 接下来是美滋滋的代码时间~~~ #include<iostream> #i ...

  5. 逻辑回归原理 面试 Logistic Regression

    逻辑回归是假设数据服从独立且服从伯努利分布,多用于二分类场景,应用极大似然估计构造损失函数,并使用梯度下降法对参数进行估计.

  6. Java基础_通过模拟售票情景解决线程不安全问题

    用代码来模拟铁路售票系统,实现通过四个售票点发售某日某次列车的100张车票,一个售票点用一个线程表示 第一种方法:通过继承Thread类的方法创建线程 package com.Gary1; publi ...

  7. CF280C

    CF280C ZR补题计划 题意: 一棵有根树,每次选择一个未删除的节点,然后删除它和它的子树内的点,问期望删多少次可以把整个树删完 解析: 显然,通过题面,我们可以知道对于一个点对 $ (u,v) ...

  8. Redis 延迟指标监控

    Redis 延迟监控框架 Redis 2.8.13 引入了Latency Monitoring的一个新功能,可以帮助我们检查和排查引起延迟的原因. Latecny Monitoring 由如下组成: ...

  9. spring boot 下 开启 gzip

    [参考文章]:Spring boot开启Gzip压缩 [参考文章]:Accept-Encoding Spring 版本 :5.1.2-RELEASE 1. application.yml 配置 ser ...

  10. Nginx之共享内存与slab机制

    1. 共享内存 在 Nginx 里,一块完整的共享内存以结构体 ngx_shm_zone_t 来封装,如下: typedef struct ngx_shm_zone_s ngx_shm_zone_t; ...