数据结构的题,从网上找到的实现方式,先记录下来。

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的更多相关文章

  1. [Swift]LeetCode707. 设计链表 | Design Linked List

    Design your implementation of the linked list. You can choose to use the singly linked list or the d ...

  2. LeetCode707:设计链表 Design Linked List

    爱写bug (ID:iCodeBugs) 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/ ...

  3. Leetcode707.Design Linked List设计链表

    设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...

  4. LeetCode707 设计链表

    设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链表,则还需要一个属性 ...

  5. LeetCode通关:听说链表是门槛,这就抬脚跨门而入

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master       https://github.com/ch ...

随机推荐

  1. tab页面自动跳转原因【在控制ul和li的时候没有细分】

    效果图 存储buy的tab跳转js代码 $(function() { $('.tabPanel ul li').click(function(){ $(this).addClass('hit').si ...

  2. 揭示同步块索引(上):从lock开始

    转自:http://www.cnblogs.com/yuyijq/archive/2009/03/13/1410071.html 大家都知道引用类型对象除实例字段的开销外,还有两个字段的开销:类型指针 ...

  3. IOS SEL (@selector) 原理及使用总结(二)

    SEL消息机制工作原理是什么 引用下面文章: 我们在之前有提到,一个类就像一个 C 结构.NSObject 声明了一个成员变量: isa. 由于 NSObject 是所有类的根类,所以所有的对象都会有 ...

  4. 一个查看Cookie的便捷工具——EditThisCookie

    Appium正在努力准备中,很快就要和大家见面了- 今天给大家分享一个查看cookies的工具,用fiddler总感觉有点麻烦,还乱七八糟的找不到到底哪个链接是当前网站的cookies: 首先,你用的 ...

  5. json对象和json字符串相互转换

    1.将JSON字符串转换为JSON对象 var data = JSON.parse(str); // JSON.parse();方法 console.log(data.name); 2.将JSON对象 ...

  6. 使用 openresty 修改请求内容

    1. 目的    动态修改 html 页面内容   2. 使用方式    openresty  在 header_filter 阶段 以及body_filter 阶段进行数据修改   3. 源码  此 ...

  7. php基础语法(控制语句、数组、函数)

    流程控制 if -else if -else语句: switch语句: while循环: do while循环 for循环: 控制脚本执行进度 die(“输出内容”) exit是die的同义词. sl ...

  8. 使用Costura.Fody将源DLL合并到目标EXE

    本文为原创文章,如转载,请在网页明显位置标明原文名称.作者及网址,谢谢! 一.本文主要是使用Costura.Fody工具将源DLL合并到目标EXE,因此,需要从以下任一链接下载: ①从Github地址 ...

  9. yum命令集

    升级相关命令: yum update : 安装所有更新软件 yum update xxx : 仅更新指定的软件 yum check-update : 列出所有可更新的软件清单 yum list : 列 ...

  10. J2EE项目在weblogic下的改动

    1.struts所有配置文件放到classes根目录下 2〉java.lang.ClassCastException:weblogic.xml.jaxp.RegistryDocumentBuilder ...