leetcode707
数据结构的题,从网上找到的实现方式,先记录下来。
class MyLinkedList {
public:
/** Initialize your data structure here. */
MyLinkedList() {
LinkedList = ;
}
ListNode *LinkedList;
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
int i = ;
ListNode *head = LinkedList;
while (head&&i<index) {
head = head->next;
i++;
}
if (head&&i == index)
return head->val;
else
return -;
}
/** 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) {
ListNode *head = (ListNode *)malloc(sizeof(ListNode));
head->next = LinkedList;
head->val = val;
LinkedList = head;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
ListNode *head = LinkedList;
ListNode *tmp = (ListNode *)malloc(sizeof(ListNode));
tmp->next = ;
tmp->val = val;
if (!head)
{
LinkedList = tmp;
return;
}
while (head->next)
{
head = head->next;
}
head->next = tmp;
}
/** 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) {
int i = ;
ListNode *head = LinkedList;
if (!head&&index==)
{
ListNode *tmp = (ListNode *)malloc(sizeof(ListNode));
tmp->val = val;
tmp->next = ;
LinkedList = tmp;
return;
}
while (head&&i<index - )
{
head = head->next;
i++;
}
if (head&&head->next == )
{
ListNode *tmp = (ListNode *)malloc(sizeof(ListNode));
tmp->val = val;
tmp->next = ;
head->next = tmp;
}
else if (i == index - && head&&head->next)
{
ListNode *tmp = (ListNode *)malloc(sizeof(ListNode));
tmp->val = val;
tmp->next = head->next;
head->next = tmp;
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
ListNode *head = LinkedList;
int i = ;
while (head&&i<index - )
{
head = head->next;
i++;
}
if (head == )
return;
if (head->next == && index == )
{
//只有一个节点的情况
LinkedList = ;
return;
}
if (head->next)
{
ListNode *tmp = head->next;
head->next = tmp->next;
free(tmp);
}
}
};
/**
* 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);
*/
leetcode707的更多相关文章
- [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 是指向下一个节点的指针/ ...
- Leetcode707.Design Linked List设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- LeetCode707 设计链表
设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...
- LeetCode通关:听说链表是门槛,这就抬脚跨门而入
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master https://github.com/ch ...
随机推荐
- erlang开发环境配置
第一步 从源码安装erlang git clone https://github.com/erlang/otp 目前最新版本为17.X cd otp/ ./configer 检查编译环境 sudo ...
- Mvc6 错误Microsoft.AspNet.Http.Features.IRequestIdentifierFeature
System.TypeLoadException 未能从程序集“Microsoft.AspNet.Http.Features, Version=1.0.0.0, Culture=neutral, Pu ...
- 如何删除 Windows 10 系统生成的 WindowsApps 文件夹
如果曾经修改过 Windows 10 应用安装路径到非系统盘,那么那个盘下就会生成一些文件夹.如果以后重装了系统或者应用删除了,挪位置了,那些文件夹依然在那里——删不掉! 大家都知道这是权限问题,然而 ...
- WPF 程序无法触摸操作?我们一起来找原因和解决方法!
WPF 自诞生以来就带着微软先生的傲慢.微软说 WPF 支持触摸,于是 WPF 就真的支持触摸了.对,我说的是"支持触摸",那种摸上去能点能动的:偶尔还能带点儿多指的炫酷效果.但是 ...
- sublime自动格式化代码插件HTML-CSS-JS Prettify安装
sublime自动格式化代码插件HTML-CSS-JS Prettify安装 问题: 用 Sublime Text 格式化代码(安装 HTML-CSS-JS Prettify 插件)时,格式化时却会提 ...
- jekyll 安装使用
1. 安装 条件: ruby gem 注意版本,同时建议使用国内的镜像 gem install jekyll bundler 2. 创建网站 jekyll new my-awesome ...
- (转)Tomcat迁移JBoss杂症—不识别及不能解析web.xml
本文介绍了在将tomcat下的web工程迁移到jboss下面时遇到的问题 背景: Tomcat 7.0 JBoss AS 4.2.2 IED: Eclipse Java EE IDE for Web ...
- 一篇文章学LINQ(原创)
本篇文章主要介绍linq的基本用法,采用sql和linq比较的方式由浅入深进行学习, 注意:此文章是根据真实表来进行案例说明,表结构如下: 表1: Student(学生表) ...
- yii2自定义500错误
由于项目想加预警监控,有一块儿是涉及到程序内部错误的500,这样的错误级别比较高,所以就需要捕获这样的错误,顺便自定义了一把视图样式 看了这篇博客,知道了如何去自定义自己错误页面 : http://t ...
- HTML第一讲
HTML标记区分 HTML即超文本标记语言(HtyperText Markup Language),其作用就是将编辑的内容在屏幕上显示.文件的后缀为.HTML. 在HTML中成对出现的叫做双标记(譬如 ...