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 是当前节点的 ...
随机推荐
- jQuery的$符号
jQuery使用$的原因是: 书写简洁, 相对于其他字符与众不同, 容易被记住. JQuery占用了两个变量: $ 和 jQuery. 当在代码中打印 $ 和 jQuery时: <script ...
- MySQL初识数据库
为什要用数据库 第一,将文件和程序存在一台机器上是很不合理的. 第二,操作文件是一件很麻烦的事 你可以理解为 数据库 是一个可以在一台机器上独立工作的,并且可以给我们提供高效.便捷的方式对数据进行增删 ...
- SecureCRT的安装与破解,详细过程
使用SecureCRT可以方便用户在windows环境下对linux主机进行管理,这里为大家讲一下SecureCRT的破解方法,仅供大家参考学习: 1.首先在本站下载SecureCRT 7.3.6最新 ...
- HDU 3689 Infinite monkey theorem ——(自动机+DP)
这题由于是一个单词,其实直接kmp+dp也无妨.建立自动机当然也是可以的.设dp[i][j]表示匹配到第i个字母的时候,在单词中处于第j个位置的概率,因此最终的答案是dp[0~m][len],m是输入 ...
- 修改checkbox样式-1
说明 使用伪类来对复选框进行样式修改.以下以最简单的一个样式修改为实例进行说明. 步骤介绍: 将一个label与复选框进行绑定,将两者放在同一个div下 调整 label的外部样式使其作为复选框的外形 ...
- 在linux写一个shell脚本用maven git自动更新代码并且打包部署
服务器上必须安装了git maven jdk 并且配置好环境变量 实际服务器中可能运行着多个Java进程,所以重新部署的时候需要先停止原来的java进程,写一个按照名称杀死进程的脚本 kill.sh ...
- Java虚拟机与平台无关性
概述 代码编译的结果从本地机器码转变为字节码,是存储格式发展的一小步,却是编程语言发展的一大步. --<深入理解Java虚拟机>(从这里可以看到,字节码是一种文件格式) 我们都知道计算机只 ...
- cloud toolkit同时部署多个服务器
首先安装cloud toolkit安装完成之后重启idea 添加host主机信息: 部署到远程服务器 具体信息: 配置本地查看服务器日志信息 启动脚本信息 start.sh #! /bin/sh so ...
- 在linux上使用impdp命令时提示ORA-12154: TNS:could not resolve the connect identifier specified的问题
今天在一台linux服务器上用impdp命令导入dmp文件时出现了错误: ORA: TNS:could not resolve the connect identifier specified 我使用 ...
- 数据结构之栈(stack)
1,栈的定义 栈:先进后出的数据结构,如下图所示,先进去的数据在底部,最后取出,后进去的数据在顶部,最先被取出. 栈常用操作: s=Stack() 创建栈 s.push(item) 将数据item放在 ...