题目:

总而言之就是要用C++手撸链表,我的代码:

class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList() {
head = NULL;
size = 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 >= size){
return -1;
}
Node *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) {
Node *newhead = new Node(val, head);
head = newhead;
size++;
} /** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
Node *tail = new Node(val, NULL);
Node *cur = head;
while(cur->next)cur = cur->next;
cur->next = tail;
size++;
} /** 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>size){
return;
}
if(index==0||index==-1){
addAtHead(val);
return;
}
if(index==size){
addAtTail(val);
return;
}
Node *cur = head;
for(int i=0; i<index-1; i++){
cur = cur->next;
}
Node *toAdd = new Node(val, cur->next);
cur->next = toAdd;
size++;
} /** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if(index<0 || index >= size) return;
Node *cur = head;
if(index == 0){
head = head->next;
size--;
delete cur;
return;
}
for(int i=0; i<index-1; i++){
cur = cur->next;
}
Node *toFree = cur->next;
cur->next = cur->next->next;
delete toFree;
size--;
} private:
struct Node{
int val;
Node *next;
Node(int x, Node *n): val(x), next(n) {};
};
Node *head;
int size;
};

这题其实很简单,这里的实现方式是单链表,基本上就是数据结构的知识点,所以没什么好提的。但是万恶的LeetCode有这么个测试样例:

也就是说,当输入的index-1时要当成0来处理,但是这一点题目里面完全没有提及。只需要改一下判定条件就可以了(把void addAtIndex(int index, int val)里的if(index==0)改成if(index==0||index==-1)),除了这个问题之外基本上没什么要注意的了。

LeetCode——707 设计链表的更多相关文章

  1. Java实现 LeetCode 707 设计链表(环形链表)

    707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...

  2. LeetCode | 707. 设计链表

    设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...

  3. LeetCode 707 ——设计链表

    1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...

  4. LeetCode——142 设计链表2

    题目 代码 class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *fast = head, *slow ...

  5. LeetCode——141 设计链表

    题目: 简单说下思路: 用两个指针,一个跑得快,一个跑得慢(例如一个每次前进两步,一个前进一步),这样只要快指针不会撞上NULL(如果遇到了NULL的情况那么必然不存在环),快指针肯定会和慢指针碰面( ...

  6. C#LeetCode刷题-链表

    链表篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 19 删除链表的倒数第N个节点   29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...

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

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

  8. [Swift]LeetCode707. 设计链表 | Design Linked List

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

  9. LeetCode707:设计链表 Design Linked List

    爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...

随机推荐

  1. InisghtFace 制作自定义数据集和模型训练评估

    前言 本文以lfw数据集进行示例 lfw结果集下载地址:http://vis-www.cs.umass.edu/lfw/lfw.tgz insightface源码下载地址:https://github ...

  2. Django模板层2

    一.单表操作 1.1 开启test from django.test import TestCase import os # Create your tests here. if __name__ = ...

  3. 2019-11-28-win10-uwp-提示-Cannot-find-a-Resource-with-the-Name-Key-找不到资源

    title author date CreateTime categories win10 uwp 提示 Cannot find a Resource with the Name Key 找不到资源 ...

  4. 数据库管理利器——Navicat Premium v12.1.25 下载和安装

    目录 1. 按 2. 新功能 3. 安装 4. 激活 5. 下载地址 1. 按 Navicat Premium 是一套数据库管理工具,让你以单一程序同時连接到 MySQL.MariaDB.SQL Se ...

  5. MongoDB与python 交互

    一.安装pymongo 注意 :当同时安装了python2和python3,为区分两者的pip,分别取名为pip2和pip3. 推荐:https://www.cnblogs.com/thunderLL ...

  6. 020-VMware虚拟机作为OpenStack计算节点,上面的虚拟机无法启动问题解决

      问题描述: VMware虚拟机作为OpenStack计算节点,如果安装的操作系统是CentOS7.3,则在此计算节点放置的虚拟机无法正常启动,报如下错误: 在创建计算节点时,为了能让 KVM 能创 ...

  7. sed 流编辑命令

    1.命令功能 sed非交互式的流编辑器,sed不会修改源文件内容,除非重定向来保存输出结果:默认情况下所有的输出行都将被打印到屏幕上. 2.语法格式 sed  [option]  {script-on ...

  8. 扩展微信小程序 Page 构造函数,修改生命周期函数

    不BB,直接正题 一. 将公共方法绑定到Page上 单个绑定 const oldPage = Page Page = function(app) { // 注意公共函数的名字不要重复,否则覆盖 app ...

  9. MySQL常见的三种存储引擎

    原文链接:https://www.cnblogs.com/yuxiuyan/p/6511837.html 简单来说,存储引擎就是指表的类型以及表在计算机上的存储方式. 存储引擎的概念是MySQL的特点 ...

  10. NOIP2017 D1T1 小凯的疑惑

    洛谷P3951 看到题目,很容易想到这一题是求使ax+by=c(a,b,c∈N)无非负整数解的最大c 由裴蜀定理可知方程一定有整数解(a,b互素,gcd(a,b)=1|c) 解法一:暴力枚举 看到题目 ...