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 是指向下一个节点的指针/ ...
随机推荐
- windows mysql密码设置与破解
1.设置密码 mysqladmin -uroot -p password "1234" 查看当前用户: 2.破解密码 关闭 MySQL 服务 执行 mysqld --skip-gr ...
- HTML中id和class选择器
<1>.id和class的区别? id相当于人的身份证不可以重复 class相当于人的名称可以重复 一个HTML标签只能绑定一个id名称 一个HTML标签可以绑定多个class名称 < ...
- React Router 4 的使用(2)
Route Rendering Props 对于给定的路由如何渲染组件,有三种选项:component.render.children.你可以查看 <Route> 的文档来获取更多的信息, ...
- win7 bat copy 一个文件 到另外的文件夹内,路径得用引号哦
win 7 的 用引号 把路径引起来 ,但是win10 的可以不用哦 !
- oracle-sql脚本导出EXCEL数据
在数据库中,经常有业务人员提出需求导出数据库中的业务数据,而且是每天.每周或每月定时导出.为了方便,可将sql查询的脚本 通过下面脚本来导出EXCEL数据. 1.将查询sql脚本(AAA.sql)放到 ...
- Spring data JPA 理解(默认查询 自定义查询 分页查询)及no session 三种处理方法
简介:Spring Data JPA 其实就是JDK方式(还有一种cglib的方式需要Class)的动态代理 (需要一个接口 有一大堆接口最上边的是Repository接口来自org.springfr ...
- JavaScript 基础(一)
基本语法: 区分大小写: ECMAScript 中的一切(变量,函数名和操作符)都区分大小写. 标识符: 表示符就是指,变量,函数,属性名字,或者函数的参数. 1.第一个字符必须是一个字母,下划线(_ ...
- Lucene 工作原理
Lucene 简介 Lucene 是一个基于 Java 的全文信息检索工具包,它不是一个完整的搜索应用程序,而是为你的应用程序提供索引和搜索功能.Lucene 目前是 Apache Jakarta 家 ...
- union的两个子查询是否并行
需求描述 问题:subquery 1 union subquery2,其中union左右的两个子查询是否并行. 场景:业务中性能敏感的业务,希望能加快速度,如果数据库能两个子查询并行执行,既可以节省时 ...
- Struts2进阶学习3
Struts2进阶学习3 OGNL表达式与Struts2的整合 核心配置文件与页面 <?xml version="1.0" encoding="UTF-8" ...