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 是指向下一个节点的指针/ ...
随机推荐
- ASP.NET MVC Cookie 身份验证
1 创建一个ASP.NET MVC 项目 添加一个 AccountController 类. public class AccountController : Controller { [HttpGe ...
- React 相关开发工具
Gulp:是一个NodeJs项目构建工具,高效易学:把一个开发中的项目构建成一个可以部署在服务器上的项目,压缩 整合 gulp.task('1',['2','3'],function(){});// ...
- input上传图片并显示
html: <div id="click"><img> </div><!--照片预览的div --> <div class=& ...
- 利用css transition属性实现一个带动画显隐的微信小程序部件
我们先来看效果图 像这样的一个带过渡效果的小部件在我们实际开发中的应用几率还是比较大的,但是在开发微信小程序的过程中可能有的小伙伴发现transition这个属性它不好使(下面说明)所以我们这个时候会 ...
- 卸载MySQL以及重装卡到Start Services的解决办法(亲测有效,刚重装成功)
卸载MySQL以及重装卡到Start Services的解决办法 重装系统永远是个好办法,但是对于我们程序员来说只要一想到电脑上的环境变量和其他的配置就蔫了.所以这一条就当作是废话吧. 一般来说装My ...
- 重置mysql5.7.25临时密码
安装完mysql之后,登陆以后,不管运行任何命令,总是提示这个:mac mysql error You must reset your password using ALTER USER statem ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--K-密码
链接:https://www.nowcoder.com/acm/contest/90/K 来源:牛客网 - 1.题目描述 ZiZi登录各种账号的时候,总是会忘记密码,所以他把密码都记录在一个记事本上. ...
- Linux的开山篇
一.Linux的学习方向 1.2Linux运维工程师 1.2.2Linux嵌入式开发工程师 1.2.3在Linux下做各种程序开发 javaEE 大数据 Python PHP C/ ...
- 【c学习-10】
#include #include #define SOURCE 0 //递归函数 /* [基本类型 [整型(int,[长整型(long int), [短整型(short int),长度整型(long ...
- JavaScript : CORS和Ajax请求
CORS(Cross-Origin Resource Sharing, 跨源资源共享)是W3C出的一个标准,其思想是使用自定义的HTTP头部让浏览器与服务器进行沟通,从而决定请求或响应是应该成功,还是 ...