#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 ...
随机推荐
- Why network port is open but no process attached?(为什么端口被打开,但是没有进程号)
When I check my system today, I noticed a weird output from netstat’s output, joseph# sudo netstat - ...
- zhengruioi 470 区间
区间 链接 题意:给定n个区间[li,ri].你可以选出任意一些区间,设选出的区间个数是s,[l,r]是这些区间的交,求min(s,r-l+1)的最大值. N≤3×105 分析:首先按照左端点排序,然 ...
- CF97C Winning Strategy
CF97C Winning Strategy 洛咕(题意应该快传上去了) 这题好玄学鸭...都不知道为啥是对的 设\(f[i][j]\)表示打了i轮比赛,有j个参加了一次的人,直接枚举有几个参加了转移 ...
- JAVA Eclipse 快捷键 ctrl+f 查找/替换 字符串
- WHO ARE YOU?--writeup
TIPS:广东强网杯线上题 总结知识点:BASE64,ROT13 0x00 Base64 什么是Base64? Base64编码原理 其用途 什么是Base64? Base64是一种基于64个可打印字 ...
- Scrapy中的POST请求发送和递归爬取
POST请求发送 重写爬虫应用文件中继承Spider类的 类的里面的start_requests(self)这个方法 def start_requests(self): #请求的url post_ur ...
- 记一次eslint规则配置
{ // 环境定义了预定义的全局变量. "env": { //环境定义了预定义的全局变量.更多在官网查看 "browser": true, "node ...
- 【SIKIA计划】_11_Unity动画插件-DOTween笔记
[插值移动]using DG.Tweening;public class GetStart:MomoBehaviour{ public Vector3 myValue = new Vector3(0, ...
- elementUI el-select 多选情况下包含全部选项,及获得选中项的label
<template> <div> <span style="margin-left:30px;font-weight:bolder;">教练: ...
- 微信小程序video视频组件
支持mp4和m3u8的视频格式,其中mp4的需要是h264的视频编码 .1.如果您使用video组件是mp4的但不能播放,大部分是由于编码的问题,当然排除文件不存在等这些客观的因素条件.2.如果使用m ...