问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4118 访问。

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

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

MyLinkedList linkedList = new MyLinkedList();

linkedList.addAtHead(1);

linkedList.addAtTail(3);

linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3

linkedList.get(1);            //返回2

linkedList.deleteAtIndex(1);  //现在链表是1-> 3

linkedList.get(1);            //返回3

提示:

所有值都在 [1, 1000] 之内。
操作次数将在  [1, 1000] 之内。
请不要使用内置的 LinkedList 库。


Design your implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

get(index) : Get the value of the index-th node in the linked list. If the index is invalid, return -1.
addAtHead(val) : 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.
addAtTail(val) : Append a node of value val to the last element of the linked list.
addAtIndex(index, 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.
deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

MyLinkedList linkedList = new MyLinkedList();

linkedList.addAtHead(1);

linkedList.addAtTail(3);

linkedList.addAtIndex(1, 2);  // linked list becomes 1->2->3

linkedList.get(1);            // returns 2

linkedList.deleteAtIndex(1);  // now the linked list is 1->3

linkedList.get(1);            // returns 3

Note:

All values will be in the range of [1, 1000].
The number of operations will be in the range of [1, 1000].
Please do not use the built-in LinkedList library.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4118 访问。

public class Program {public static void Main(string[] args) {var linkedList = new MyLinkedList();linkedList.AddAtHead(1);linkedList.AddAtTail(3);linkedList.AddAtIndex(1, 2);Console.WriteLine(linkedList.Get(1));linkedList.DeleteAtIndex(1);Console.WriteLine(linkedList.Get(1));Console.ReadKey();}public class ListNode {public int val;public ListNode next;public ListNode(int x) { val = x; }}public class MyLinkedList {private ListNode _listNode = null;private int _count = 0;public MyLinkedList() {_listNode = new ListNode(0);}public int Get(int index) {if(index < 0 || index >= _count) return -1;var idx = 0;var node = _listNode;while(idx++ != index) {node = node.next;}return node.val;}public void AddAtHead(int val) {if(_count == 0) {_listNode.val = val;} else {var newHead = new ListNode(val);newHead.next = _listNode;_listNode = newHead;}_count++;}public void AddAtTail(int val) {AddAtIndex(_count, val);}public void AddAtIndex(int index, int val) {if(index < 0 || index > _count) return;if(index == 0) {var newHead = new ListNode(val);newHead.next = _listNode;_listNode = newHead;_count++;return;}var pre = _listNode;var idx = 0;while(idx++ != index - 1) {pre = pre?.next;}if(pre == null) return;var temp = pre.next;pre.next = new ListNode(val);pre.next.next = temp;_count++;}public void DeleteAtIndex(int index) {if(index < 0 || index >= _count) return;var pre = _listNode;var idx = 0;while(idx++ != index - 1) {pre = pre.next;}pre.next = pre.next.next;_count--;}}}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4118 访问。

2
3

分析:

这道题在LeetCode为简单,基础知识也很简单,但想要AC真的要花不少功夫,各种边界的处理,另外可以抽出一个独立的Find方法用于查找某索引处的ListNode,各种看官有兴趣的自己尝试一下。

显而易见,Get、AddAtTailAdd、AtIndex、DeleteAtIndex 的时间复杂度为:  ,AddAtHead 的时间复杂度为:  。

C#LeetCode刷题之#707-设计链表(Design Linked List)相关推荐

  1. 卷进大厂系列之LeetCode刷题笔记:设计链表(中等)

    学算法,刷力扣,加油卷,进大厂! 题目描述 力扣题目链接 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下 ...

  2. C#LeetCode刷题之#141-环形链表(Linked List Cycle)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3901 访问. 给定一个链表,判断链表中是否有环. 进阶: 你能否 ...

  3. LeetCode刷题 --杂篇 --数组,链表,栈,队列

    武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...

  4. 【leetcode刷题】16.环形链表——Java版

    ⭐欢迎订阅<leetcode>专栏,每日一题,每天进步⭐ 使用快慢指针,若指针相遇则判断有环 --leetcode此题热评 前言 哈喽,大家好,我是一条. 糊涂算法,难得糊涂 Questi ...

  5. 如何判断链表中是否存在环?Floyd判圈算法 leetcode刷题笔记 142. 环形链表 II

    这道题使用了floyd判圈算法,所以先讲解floyd算法的原理和实现,最后在附加上第142题的代码. floyd算法: 一.用途: 可以在有限状态机.迭代函数或者链表上判断是否存在环,求出该环的起点与 ...

  6. 卷进大厂系列之LeetCode刷题笔记:反转链表(简单)

    学算法,刷力扣,加油卷,进大厂! 题目描述 力扣题目链接 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表. 示例 1: 输入:head = [1,2,3,4,5] 输出:[5,4,3 ...

  7. C#LeetCode刷题之#234-回文链表(Palindrome Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3905 访问. 请判断一个链表是否为回文链表. 输入: 1-> ...

  8. C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3824 访问. 编写一个程序,找到两个单链表相交的起始节点. 例如 ...

  9. leetcode刷题 82.删除排序链表中的重复元素Ⅱ

    题目解析: 因为本题是使用链表,且题目要求删除连续重复数字,由此可以想到我们应该使用双指针的方法. 代码实现: /*** Definition for singly-linked list.* str ...

最新文章

  1. 重磅!2020中国高校毕业生月薪排名:清华第1,共计24高校月薪过万
  2. 人工智能,“抛弃”真实数据集?
  3. Mock工具之Mockito实战
  4. Web 文件管理器elFinder 的漏洞链分析
  5. python 实现ping测试延迟的两种方法
  6. 目标、物体识别(检测)object detection 中的 bounding boxes 是什么? 边界区域、边界框、边界盒
  7. 对计算器软件的测试报告,Win7计算器软件测试报告.doc
  8. 红黑树(Red-Black Tree)
  9. 传统城域网架构遇瓶颈 引入NFV成有效解法
  10. MongoDB数据库练习题
  11. 4r照片尺寸是多大_4寸照片尺寸多少厘米 多少像素
  12. hp台式计算机u盘启动设置,hp惠普品牌机设置u盘启动教程
  13. Android 之自定义表情
  14. 游戏原画,掌握角色设计2个小技巧,你也能画好角色
  15. canvas节点无法导出图片_HTML转为PDF,图片导出失败的终极解决方案
  16. 算法(Java)——双指针
  17. 笔记本屏幕 亮度 背光调节 工具 c++ 用来解决亮度调节功能键失效问题
  18. 数据库课程设计----学生信息与选课、成绩评价管理系统
  19. 深度学习基础-损失函数详解
  20. 前端优化之webpack

热门文章

  1. 如何用Pygame写游戏(四)
  2. js学习笔记(十二)——语法速查表
  3. 【C++深入探索】Copy-and-swap idiom详解和实现安全自我赋值
  4. 华科08年计算机考研复试机试
  5. 数组的升序排序 字符串的方法 0308
  6. 1910101811-2
  7. python-对向-查看全部属性-查看全部方法
  8. Mysql Group Replication(MGR)搭建
  9. GitLab 简易指引(三):备份与恢复
  10. connect.js源码解析