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

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. js之简易计算器

    <!DOCTYPE html PUBLIC "-//W3C//Dli XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Why I am not afraid of AI (TBC)

    Freud! Yes, according to Freud's theory, most human activities are driven by libido (or aim-inhibite ...

  3. 线性回归 Linear regression(1)线性回归的基本算法与求解

    本系列内容大部分来自Standford公开课machine learning中Andrew老师的讲解,附加自己的一些理解,编程实现和学习笔记. 第一章 Linear regression 1.线性回归 ...

  4. BZOJ1030 JSOI2007 文本生成器 【AC自动机】【DP】*

    BZOJ1030 JSOI2007 文本生成器 Description JSOI交给队员ZYX一个任务,编制一个称之为"文本生成器"的电脑软件:该软件的使用者是一些低幼人群,他们现 ...

  5. 实现一个 WPF 版本的 ConnectedAnimation

    Windows 10 的创造者更新为开发者们带来了 Connected Animation 连接动画,这也是 Fluent Design System 的一部分.它的视觉引导性很强,用户能够在它的帮助 ...

  6. Working out

    Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...

  7. 《selenium2 python 自动化测试实战》(15)——调用js控制滚动条等操作

    看代码: # coding=utf-8 from time import sleepfrom selenium import webdriver driver = webdriver.Firefox( ...

  8. callback回调函数-python

    链接:http://www.zhihu.com/question/19801131/answer/27459821来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 编程分 ...

  9. ballerina 学习十五 控制流

    ballerina 的控制流没有什么特殊,只是相比一般语言多了一个模式匹配的操作match ,实际上其他语言(erlang elixir rust 中的模式匹配是很强大的) 简单例子 if/else ...

  10. 7个GIF动图帮你瞬间理解三角函数

    7个GIF动图帮你瞬间理解三角函数 蝌蚪五线谱 百家号04-2120:53 图片来源:IMGUR 三角函数是数学中研究三角形的一个分支,专门阐述三角形的角度和对应边的关系. 有趣的是,定义边角关系的三 ...