#Leetcode# 707. Design Linked List
https://leetcode.com/problems/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. - 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
Note:
- All values will be in the range of
[1, 1000]
. - The number of operations will be in the range of
[1, 1000]
. - Please do not use the built-in LinkedList library.
代码:
class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList() {
head = NULL;
sz = 0;
} /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if(index < 0 || index >= sz) return -1;
ListNode *cur = head;
for(int i = 0; i < index; i ++) cur = cur -> next;
return cur -> 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. */
void addAtHead(int val) {
ListNode *pre = new ListNode(val, head);
head = pre;
sz ++;
} /** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
ListNode *pre = head;
while(pre -> next) pre = pre -> next;
pre -> next = new ListNode(val, NULL);
sz ++;
} /** 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. */
void addAtIndex(int index, int val) {
if(index < 0 || index > sz) return;
if(index == 0) {
addAtHead(val);
return;
}
ListNode *pre = head;
for(int i = 0; i < index - 1; i ++) pre = pre -> next;
ListNode *cur = new ListNode(val, pre -> next);
pre -> next = cur;
sz ++;
} /** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if(index < 0 || index >= sz) return;
if(index == 0) {
head = head -> next;
sz --;
return;
} ListNode *pre = head;
for(int i = 0; i <index - 1; i ++) pre = pre -> next;
pre -> next = pre -> next -> next;
sz --;
}
private:
struct ListNode {
int val;
ListNode *next;
ListNode(int x, ListNode *n): val(x),next(n) {}
};
ListNode *head, *tail;
int sz;
}; /**
* 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);
*/
这个写完之后仿佛已经搞清楚了链表 但愿不是错觉叭
#Leetcode# 707. Design Linked List的更多相关文章
- LeetCode 707. Design Linked List (设计链表)
题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...
- 【Leetcode_easy】707. Design Linked List
problem 707. Design Linked List 参考 1. Leetcode_easy_707. Design Linked List; 完
- 【LeetCode】707. Design Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- 707. Design Linked List
1. 原始题目 Design your implementation of the linked list. You can choose to use the singly linked list ...
- [LeetCode] 641.Design Circular Deque 设计环形双向队列
Design your implementation of the circular double-ended queue (deque). Your implementation should su ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
随机推荐
- 柯朗微积分与数学分析习题选解(1.1 节 a)
一直在读<陶哲轩实分析>,陶的书非常的严谨,环环相扣,但是也有个缺点就是计算性的例子和应用方面的例子太少了.所以就又找了本柯朗的<微积分与数学分析>搭配着看.柯朗的书的习题与陶 ...
- 24-[jQuery]-属性,文档,位置,筛选
1.jquery的属性操作 jquery对象有它自己的属性和方法,我们先研究一下jquery的属性操作.jquery的属性操作模块分为四个部分:html属性操作,dom属性操作,类样式操作和值操作 h ...
- Gitlab+Jenkins学习之路(十一)之Jenkins自动触发构建和发布
思路图: 一.下载gitlab plugin jenkins-->系统管理-->管理插件-->下载并安装gitlab plugin 二.配置gitlab认证 路径:Jenkins-- ...
- angularJs中缓存数据,免去重复发起请求的几种写法
带缓存处理的两种写法 过程:点击button触发load()方法,请求数据成后显示到页面中.如果已经请求过则从缓存中读取. 在线浏览 写法1: function demo(){ if (demo.ca ...
- idea web项目debug模式实时更新按钮不生效原因
必须两个都开启才能生效,单按按钮不能生效,但是有时候自动更新不生效的时候按按钮后可以生效, 如果前端目录或后端内容实在不更新,就删掉out目录和target目录,重新启动服务器即可
- 使用VS Code新建编译Flutter项目
本文的前提是你已经安装好了VS Code,并且安装了Flutter和Dart扩展插件. 1. 新建Flutter项目 查看——命令面板,或者Ctrl + Shift + P 输入 Flutter: N ...
- selenium select 选择下拉框
实战百度首页设置,浏览偏好设置. 打开首页,在非登录的情况下,查看分析页面元素,我们可以看到,我们首先要点击的是设置, 接着点击,搜索设置, 然后select选择下拉框. select_by_inde ...
- 用CSS3做3D动画的那些事
年会做了个3D变换的抽奖系统,在这里分享下通过CSS3制作3D效果的心得.抽奖系统虽然够炫酷,可惜抽的时候出了点bug,好几百人啊我的小心脏啊.虽然这个锅后面甩给会场的老爷电脑了(手动白眼). 首先介 ...
- WinForm 捕获系统关机、重启、注销事件
Public Class App Public Shared Sub Main() ' 关联事件 AddHandler Microsoft.Win32.SystemEvents.SessionEndi ...
- linux用命令行编译使用函数库
同步于气象家园日志 from fcode 视频 编译静态链接库 gfortran -c sub.f90 func.f90 产生了func.mod文件.注:mod文件是静态库的接口.如果删掉了fu ...