题目:

总而言之就是要用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. Java web项目搭建系列之二 Jetty下运行项目

    在项目pom.xml文件中添加Jetty运行配置 在pom.xml文件project节点下插入如下代码: <build> <plugins> <plugin> &l ...

  2. Thinkphp5 自定义分页样式显示页码和数量

    Thinkphp5 自带的分页比较简单,本文通过修改Bootstrap类自定义显示分页的页码和数量 一.修改完成后如下图显示 二.修改Bootstrap代码: 1.为了不改动Bootstrap.php ...

  3. 内置的re模块

    re(正则表达式) 字符匹配: 普通字符匹配:re.findall("alex","shfalexjaf"),直接查找符合的字符 元字符:  .  ^ $ * ...

  4. 前端错误监控上报公共方法,可在父页面及iframe子页面同时使用

    先创建公共文件error-reported.js 内容如下: /** * 获取前端错误信息进行上报 * @param iframe */ function catchError (iframe) { ...

  5. 写了一个简单可用的IOC

    根据<架构探险从零开始写javaweb框架>内容写的一个简单的 IOC 学习记录    只说明了主要的类,从上到下执行的流程,需要分清主次,无法每个类都说明,只是把整个主线流程说清楚,避免 ...

  6. 如何解决Bootstrap中分页不能居中的问题

    尝试过1.text-align:center居中:2.margin:0 auto; 3.display: flex;justify-content: center;都不行 解决: 在外层多加一个nav ...

  7. Python---tkinter---贪吃蛇(能用的)

    项目分析:构成:蛇 Snake食物 Food世界 World蛇和食物属于整个世界  class World:      self.snake      self.food上面代码不太友好我们用另外一个 ...

  8. spark-2.1.1 yarn(高可用)搭建

    一.概述 spark分布式搭建方式大致分为三种:standalone.yarn.mesos.三种分类的区别这里就不一一介绍了,不明白可自行了解.standalone是官方提供的一种集群方式,企业一般不 ...

  9. STM32开发板的TIM3开启和关闭

    关闭定时器中断要考虑好多情况 1)关闭定时器时,定时器是否在处在工作状态 2)关闭定时器时,定时器是否正好进入中断,造成关闭程序出现断层,进而无法实现完整关闭程序,此时可以使用高一级别的外部中断强制进 ...

  10. getCurrentPages

    解释:getCurrentPages 全局函数用于获取当前页面栈的实例,以数组形式按栈的顺序给出,第一个元素为首页,最后一个元素为当前页面. 示例: // index.jsPage({ onShow( ...