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 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.
  • 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的更多相关文章

  1. LeetCode 707. Design Linked List (设计链表)

    题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...

  2. 【Leetcode_easy】707. Design Linked List

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

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

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

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

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

  5. 707. Design Linked List

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

  6. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  7. 【LeetCode】链表 linked list(共34题)

    [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...

  8. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  9. [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. 20145207《Java程序设计》实验四( Android程序设计)实验报告

    <Java 程序设计>实验四( Android程序设计)实验报告 目录 改变 Android开发基础实验要求 实验成果 课后思考 改变 修改了之前仅仅是贴了图片,连代码都没粘的状态.增加了 ...

  2. 单元测试——隔离神器:mockito

    mockito,一个让人着迷的单元测试隔离框架.对比了easymock,jmock,jmockito,最终选择了它. 为什么用他 接口语法简洁.自然.写起来像在说话,很舒服. 文档更完整.让学习曲线更 ...

  3. 一键将 Python2 代码自动转化为 Python3

    问题 Python2 的代码直接在 Python3 环境运行的话会报错误: 如果大量的代码,无论是批量替换,还是逐行修改都够累的,这活儿表示不能干! 有没有办法一键转换呢? 百度了一下发现网上的方法如 ...

  4. [VB.NET][C#]调用API获取或设置键盘按键状态

    前言 通过 C# 或 VB.NET,你只需编写少量的代码即可实现一个按键精灵. 第一节 接口 调用系统 API 实现获取或设置指定的按键状态. 获取按键状态 调用 GetAsyncKeyState() ...

  5. .net如何发送格式化的文本内容

    MailMessage mailMessage = new MailMessage();ArrayList attachsendObject = new ArrayList();string mail ...

  6. 百度ueditor 文本框

    所需配置(qui框架) <!--ueEditor编辑器start--> <script>  window.UEDITOR_HOME_URL = ctx+"/stati ...

  7. c语言数字图像处理(十):阈值处理

    定义 全局阈值处理 假设某一副灰度图有如下的直方图,该图像由暗色背景下的较亮物体组成,从背景中提取这一物体时,将阈值T作为分割点,分割后的图像g(x, y)由下述公式给出,称为全局阈值处理 多阈值处理 ...

  8. Python之Django的Model详解

    一.创建数据库 创建数据库 进入数据库: mysql -uroot -p 创建数据库: CREATE DATABASE test1 CHARSET=utf8; 连接数据库 虚拟环境中安装数据库模块:p ...

  9. [Processing]点到线段的最小距离

    PVector p1,p2,n; float d = 0; void setup() { size(600,600); p1 = new PVector(150,30);//线段第一个端点 p2 = ...

  10. SQL注入原理&分类&危害&防御

    SQL是什么? 结构化查询语句 SQL注入是什么? 是一种将SQL 语句插入或添加到用户输入的参数中,这些参数传递到后台服务器,加以解析并执行 造成注入的原因/原理? 1.对用户输入的参数没有进行严格 ...