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 是指向下一个节点的指针/ ...
随机推荐
- HTML5标签应用
<!doctype html> <html> <head> <style> /* *{border:1px solid red;height:20px} ...
- js中的AJAX
AJAX:Asynchronous JavaScript and XML.意思就是用JavaScript执行异步网络请求. 如果仔细观察一个Form的提交,你就会发现,一旦用户点击Submit按钮,表 ...
- django-多表操作2
#######多表操作二######## 昨天写了基于双下划线查找,都是两个表之间查找,那再多跨几个表呢?还是一样,一步一步分析 # 跨多表查询: 查询红楼梦这本书的作者的电话: (Author,Bo ...
- POCO TCPServer 解析
POCO C++ Libraries提供一套 C++ 的类库用以开发基于网络的可移植的应用程序,功能涉及线程.文件.流,网络协议包括:HTTP.FTP.SMTP 等,还提供 XML 的解析和 SQL ...
- 在Python中使用正则表达式去掉字符串里的html标签
有时候会获得一些带html标签的字符串,需要把html标签去掉,获得干净的字符串,这时候可以使用正则表达式. 代码如下: import re htmeString = '''<ul id=&qu ...
- 课时102.CSS精灵图(掌握)
我们这节课来介绍一个和背景图片相关的东西,精灵图 1.设么是css精灵图? css精灵图是一种图像合成技术 2.css精灵图作用 可以减少请求的次数,以及可以降低服务器处理压力 3.如何使用css精灵 ...
- webBrowser 应用编程函数总结
/*============================说明部分================================= 实现一下函数需包含头文件 #include <Winine ...
- tarnado源码解析系列一
目录 tarnado tarnado源码安装 tarnado测试程序 application类的解析 一. tarnado简介 最近在学习Python,无意间接触到的tarnado,感觉tarnado ...
- narcissus
public class narcissus { public static void main(String args[]) { long u=0,t=0,h=0,y=0,k=0; for(long ...
- java 堆栈内存分析详解
计算机术语里面堆和栈代表不同的存储结构:stack-栈:heap-堆 所以java虚拟机(JVM)中堆和栈是两种内存 堆.栈对比 对比点 堆 栈 JVM中的功能 内存数据区 内存指令区 动静态 运行时 ...