LeetCode 707 ——设计链表
1. 题目

2. 解答
用一个单链表来实现,只有一个头指针。因为不能建立哨兵结点,因此要特别注意是否在头结点处操作。
class MyLinkedList {
public:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *head;
/** Initialize your data structure here. */
MyLinkedList() {
head = NULL;
}
/** 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 || head == NULL) return -1;
int i = 0;
ListNode *temp = head;
while(i < index && temp->next != NULL)
{
temp = temp->next;
i++;
if (i == index) break;
}
if (i == index) return temp->val;
else return -1;
}
/** 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) {
if (head == NULL)// 原链表没有结点
{
head = new ListNode(val);
}
else // 原链表有结点
{
ListNode *temp = new ListNode(val);
temp->next = head;
head = temp;
}
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
ListNode *temp = head;
if(temp == NULL) temp = new ListNode(val); // 原链表没有结点
else
{
// 原链表有结点,先找到到链尾再添加
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new ListNode(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. */
void addAtIndex(int index, int val) {
if (index < 0) return;
if (head == NULL)
{
if (index > 0)
{
return;
}
else
{
head = new ListNode(val);
return;
}
}
int i = 0;
ListNode *temp = head;
ListNode *last = NULL;
// 查找待插入结点及其上一个结点
while(i < index && temp->next != NULL)
{
last = temp;
temp = temp->next;
i++;
if (i == index) break;
}
if (i == index) // 在待插入结点前插入
{
if (i == 0) // 在头结点后插入
{
temp = new ListNode(val);
temp->next = head;
head = temp;
}
else // 在其他位置插入
{
last->next = new ListNode(val);
last->next->next = temp;
}
}
else if (i == index - 1) // 在链表尾插入
{
temp->next = new ListNode(val);
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if (index < 0 || head == NULL) return;
int i = 0;
ListNode *temp = head;
ListNode *last = NULL;
// 查找待删除结点及其上一个结点
while(i < index && temp->next != NULL)
{
last = temp;
temp = temp->next;
i++;
if (i == index) break;
}
if (i == index)
{
if (i == 0) // 删除头结点
{
delete head;
head = NULL;
}
else // 删除其它结点
{
last->next = temp->next;
delete temp;
}
}
}
};
/**
* 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);
*/
获取更多精彩,请关注「seniusen」!

LeetCode 707 ——设计链表的更多相关文章
- Java实现 LeetCode 707 设计链表(环形链表)
707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...
- LeetCode | 707. 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- LeetCode——707 设计链表
题目: 总而言之就是要用C++手撸链表,我的代码: class MyLinkedList { public: /** Initialize your data structure here. */ M ...
- 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 是指向下一个节点的指针/ ...
随机推荐
- 【Oracle】三个配置文件tnsnames-listener-sqlnet介绍【转】
转自:博客园-oracle: listener.ora .sqlnet.ora .tnsnames.ora的配置及例子 介绍三个配置文件 1)listener.ora 2)sqlnet.ora 3)t ...
- 到底什么时候需要使用 final
final: final修饰属性,则该属性不可再次改变,而且在初始化中必须在属性或者是构造方法中其中且中有一个中初始化他 final修饰方法,则该方法不可被重写 final修饰类,则不可被继承 1:当 ...
- TensorFlow安装环境的误区
安装py一定要注意安装的版本,我一开始安装的3.7版本的,现在还没有支持,另外,看清楚自己电脑是32位还是64位的
- Vue 封装的组件生命周期钩子
export default { // ... // 在组件初始化时调用,可以简单理解为页面加载时 created () { // 存在 localStorage 的缓存内容 if (localSto ...
- python2.7打包环境配置
目前python3.x正大行其道,不过有些公司依然使用python2.x,比如说我现在的公司.网上python2.x解决方案还是有些空缺,需要自己去查找. 公司的电脑安装的python2.7,pip也 ...
- 选课(树形DP)
题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...
- 困扰我的c++语法
以下是我上周学习c++ primer的心得: 1 数组引用作为形参 c++允许将变量定义成数组的引用,以下列代码为例.形参为int (&arr)[10],该参数需分成两部分说明,引用名和 引用 ...
- [洛谷P1390]公约数的和·莫比乌斯反演
公约数的和 传送门 分析 这道题很显然答案为 \[Ans=\sum_{i=1}^n\sum_{j=i+1}^n (i,j)\] //其中\((i,j)\)意味\(gcd(i,j)\) 这样做起来很烦, ...
- CentOS7——vi编辑保存
按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :wq 保存文件并退出vi :wq! 强制保存文件, ...
- JDK5后的特性整理
为了大家对JDK有一个全面的了解,下面是我从网上查找并整理了JDK5以后的所有关键新特性!(将会持续更新中) JDK5新特性 自动装箱与拆箱 枚举 静态导入 可变参数(Varargs) 内省(intr ...