LeetCode——707 设计链表
题目:

总而言之就是要用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 设计链表的更多相关文章
- Java实现 LeetCode 707 设计链表(环形链表)
707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...
- LeetCode | 707. 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- LeetCode 707 ——设计链表
1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...
- LeetCode——142 设计链表2
题目 代码 class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *fast = head, *slow ...
- LeetCode——141 设计链表
题目: 简单说下思路: 用两个指针,一个跑得快,一个跑得慢(例如一个每次前进两步,一个前进一步),这样只要快指针不会撞上NULL(如果遇到了NULL的情况那么必然不存在环),快指针肯定会和慢指针碰面( ...
- C#LeetCode刷题-链表
链表篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 19 删除链表的倒数第N个节点 29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...
- 【LeetCode】Design Linked List(设计链表)
这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...
- [Swift]LeetCode707. 设计链表 | Design Linked List
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- LeetCode707:设计链表 Design Linked List
爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...
随机推荐
- MFC学习笔记3---使对话框风格与系统统一
有一件郁闷了我很久的事情,在VS中编辑对话框或者点击预览时都是以Win7风格体现的按钮及对话框: 点击上图测试对话框: 然而生成的应用程序却是这样的: 这样人很不爽啊,按钮风格回到了N年前的版本,复古 ...
- windows下挂载NFS共享目录
1.在打开或关闭Windows功能中,选择安装NFS客户端 2.在命令行中,输入“mount \\172.24.184.31\data x:\”,输入mount查看详细挂载参数(注意此时uid.gid ...
- rev 反向输出文件内容
1.命令功能 rev 按行反向输出文件内容 2.语法格式 rev file 3.使用范例 [root@localhost ~]# echo {a..k} >> test [root@lo ...
- 通过form提交 django的安全机制
通过form提交 在form表单里面需要添加{%csrf_token%} 这样当你查看页面源码的时候,可以看到form中有一个input是隐藏的 总结原理:当用户访问login页面的时候,会生成一个c ...
- 自己动手实现一个网络模型 python
转自:https://www.jianshu.com/p/4b30e1dd2252 common_funcs.py import numpy as np import matplotlib.pyplo ...
- linux运维、架构之路-Docker快速入门
一.Docker介绍 Docker是Docker.lnc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在Github上,基于Go语言并遵从Apache2.0 ...
- Redis实战(十一)Redis面试题
序言 单线程的redis为什么这么快? 1.纯内存操作不需要进行磁盘的 IO 2.单线程操作避免了频繁上下文切换 3.采用非阻塞的多路I/O复用模型 什么是路I/O复用模型? 核心是监听socket, ...
- GeoServer-2.12安装MbTiles扩展插件
- postman之请求&断言
http://www.jianshu.com/p/dd0db1b13cfc ---参考网址 文档:https://www.v2ex.com/p/7v9TEc53 api地址:https://ww ...
- sql server 高版本数据还原到低版本sql server的注意事项
生成的sql脚本不能大于100m,否则会报错(System.OutOfMemory) 所以要将较大的sql脚本文件进行分割