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. # 2017-2018-1 20155302 课下实践IPC及课上补充

    课上实践补交 题目二要求: 学习使用stat(1),并用C语言实现 提交学习stat(1)的截图 man -k ,grep -r的使用 伪代码 产品代码 mystate.c,提交码云链接 测试代码,m ...

  2. 苏州Uber优步司机奖励政策(4月21日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 基于bootstrap的文本编辑器组件:Summernote

    Summernote官网地址 :https://summernote.org/ 这是官网的一个例子: <!DOCTYPE html> <html lang="en" ...

  4. 【转载】完成C++不能做到的事 - Visitor模式

    原文: 完成C++不能做到的事 - Visitor模式 拿着刚磨好的热咖啡,我坐在了显示器前.“美好的一天又开始了”,我想. 昨晚做完了一个非常困难的任务并送给美国同事Review,因此今天只需要根据 ...

  5. Java Swing:JPanel中添加JPanel

    1. JPanel默认布局是FlowLayout,如果不设置父JPanel的布局,则子JPanel自动填满父JPanel. JPanel jpanel = new JPanel(); jpanel.s ...

  6. python之进程池与线程池

    一.进程池与线程池介绍 池子使用来限制并发的任务数目,限制我们的计算机在一个自己可承受的范围内去并发地执行任务 当并发的任务数远远超过了计算机的承受能力时,即无法一次性开启过多的进程数或线程数时就应该 ...

  7. 使用Sublime Text 3作为React Native的开发IDE

    1.下载安装Sublime 3 Sublime 3的下载地址:http://www.sublimetext.com/3 选相应的平台进行下载,安装. 2.安装Package Control 默认的Su ...

  8. Docker Manager for Kubernetes

    一.Kubernetes介绍 Kubernets是Google开源的容器集群系统,是基于Docker构建一个容器的调度服务,提供资源调度,均衡容灾,服务注册,动态伸缩等功能套件: Kubernets提 ...

  9. [算法总结] 20 道题搞定 BAT 面试——二叉树

    本文首发于我的个人博客:尾尾部落 0. 几个概念 完全二叉树:若二叉树的高度是h,除第h层之外,其他(1~h-1)层的节点数都达到了最大个数,并且第h层的节点都连续的集中在最左边.想到点什么没?实际上 ...

  10. uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)"解决办法

    如果在编译MFC程序的时候出现下列及类似的错误: 1>uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator ...