Design Linked List
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 next
. val
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 theindex
-th node in the linked list. Ifindex
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的更多相关文章
- 【Leetcode_easy】707. Design Linked List
problem 707. Design Linked List 参考 1. Leetcode_easy_707. Design Linked List; 完
- [Swift]LeetCode707. 设计链表 | Design Linked List
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- [LeetCode] Design Linked List 设计链表
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- 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
https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...
- LeetCode707:设计链表 Design Linked List
爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...
- C#LeetCode刷题之#707-设计链表(Design Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4118 访问. 设计链表的实现.您可以选择使用单链表或双链表.单链 ...
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
随机推荐
- bbs--点赞
bbs---点赞 需求分析 页面展示 1 点赞 和 踩灭 按钮展示 1 用户未登录,不处理点赞踩灭,给用户提供登录接口 2 登录 1 第一次点点赞/踩灭 1 点赞成功 数据+1 提示点赞成功 ...
- Digit Division(排列组合+思维)(Gym 101480D )
题目链接:Central Europe Regional Contest 2015 Zagreb, November 13-15, 2015 D.Digit Division(排列组合+思维) 题解: ...
- Druid数据源监控配置
在web.xml中添加如下代码 <!-- druid监控 --> <servlet> <servlet-name>DruidStatView</servlet ...
- 数据结构-用C++实现一个二叉树,递归方法中序遍历
1:二叉排序树,又称二叉树.其定义为:二叉排序树或者空树,或者是满足如下性质的二叉树. (1)若它的左子树非空,则左子树上所有节点的值均小于根节点的值. (2)若它的右子树非空,则右子树上所有节点的值 ...
- quartz中的corn表达式
一个Quartz的CronTrigger表达式分为七项子表达式,其中每一项以空格隔开,从左到右分别是:秒,分,时,月的某天,月,星期的某天,年:其中年不是必须的,也就是说任何一个表达式最少需要六项! ...
- LNMP和LAMP的搭建
LNMP 环境:阿里云ubuntu 16 mysql: apt-get install mysql-server mysql-client php: apt-get install php-fpm p ...
- linux安装jdk1.8之后报错Error: dl failure on line 893的解决办法
问题描述:安装jdk1.8之后,输入java -version查看安装是否成功之后,报错: 报错如下: Error: dl failure on line 893 Error: failed /u ...
- 微服务中的CAP定律
说到微服务,先给大家提一下CAP分布式应用知识吧,无论你微服务使用的是阿里云开源的Dubbo还是基于Springboot的一整套实现微服务的Springcloud都必须遵循CAP定理不然你所实现的分布 ...
- WebLogic服务器
WebLogic是美国Oracle公司出品的一个application server确切的说是一个基于JAVAEE架构的中间件,BEA WebLogic是用于开发.集成.部署和管理大型分布式Web应用 ...
- manifest节点
xmlns:android属性——定义命名空间 这个属性定义了这个XML文件所使用的命名空间.如果需要指定特殊的命名空间,就需要手动编写代码,在Android Studio基本格式如下: xmlns: ...