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 是指向下一个节点的指针/ ...
随机推荐
- 整理下react中常见的坑
其实有些也不能算是坑,有些是react的规定,或者是react的模式和平常的js处理的方式不同罢了 1.setState()是异步的this.setState()会调用render方法,但并不会立即改 ...
- Openresty最佳案例 | 第7篇: 模块开发、OpenResty连接Redis
转载请标明出处: http://blog.csdn.net/forezp/article/details/78616714 本文出自方志朋的博客 Lua模块开发 在实际的开发过程中,不可能把所有的lu ...
- CentOS 7 安装oracle 11.2.0.4 Error in invoking target 'agent nmhs' of makefile
%86时出现报错 Error in invoking target 'agent nmhs' of makefile 解决方案在makefile中添加链接libnnz11库的参数修改$ORACLE ...
- http返回值含义
1xx:信息响应类,表示接收到请求并且继续处理2xx:处理成功响应类,表示动作被成功接收.理解和接受 3xx:重定向响应类,为了完成指定的动作,必须接受进一步处理4xx:客户端错误,客户请求包含语法错 ...
- Sass 基础(七)
Sass Maps 的函数-map-remove($map,$key),keywords($args) map-remove($map,$key) map-remove($map,$key)函数是用来 ...
- Web前端几种常见的实现水平垂直居中的方法
第一种: 父容器不设置宽度,用定位实现水平垂直居中. <!DOCTYPE html> <html lang="en"> <head> <m ...
- 对象API
遍历对象里的每个元素 var obj ={ a:32, b:12, c :342 } for (const key of obj){ if(obj.hasOwnProperty(key)){ cons ...
- docker搭建基于percona-xtradb-cluster方案的mysql集群
一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...
- 访问本地方站出现EOF的分析和解决
每天早晨打开电脑运行本地项目的时候,有时候浏览器上会出现EOF 之前都都能正常访问,所以我猜想本地的项目本身肯定是没有问题的. Google了下,发现有人说是代理的问题,于是关闭代理试过后,发现可以访 ...
- 因为之前完全没有接触过Spring,所以准备先把spring实战看完再落实项目
因为之前完全没有接触过Spring,所以准备先把spring实战看完再落实项目