问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. C#LeetCode刷题之#141-环形链表(Linked List Cycle)

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

  2. LeetCode刷题总结-栈、链表、堆和队列篇

    本文介绍LeetCode上有关栈.链表.堆和队列相关的算法题的考点,推荐刷题20道.具体考点分类如下图: 一.栈 1.数学问题 题号:85. 最大矩形,难度困难 题号:224. 基本计算器,难度困难 ...

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

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

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

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

  5. LeetCode707:设计链表 Design Linked List

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

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

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

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

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

  8. LeetCode刷题之合并排序链表

    合并两个有序链表并返回一个新的列表.新列表应该由连接在一起的节点前两个列表 给定实例:Input: 1->2->4, 1->3->4Output: 1->1->2- ...

  9. C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...

随机推荐

  1. BUUCTF-web web1 (无列名注入)

    注册并登录后发现,sql注入,注入点在广告申请的界面.加单引号发现报错 先通过insert插入数据,然后再通过id查询相应的数据,所以是二次注入. 常见报错函数updatexml,floor以及ext ...

  2. 开源项目推荐 - 巨鲸任务调度平台(Spark、Flink)

    Big Whale(巨鲸),为美柚大数据研发的大数据任务调度平台,提供Spark.Flink等离线任务的调度(支持任务间的依赖调度)以及实时任务的监控,并具有批次积压告警.任务异常重启.重复应用监测. ...

  3. bubble排序

    故事的起因:好久没有用bubble了,,,居然忘记了基本格式........ 经过:,,,,这可算是我学的第一个比较有用的"算法"啊...这怎么行! 结果: void bubbleSort (elem ...

  4. Python三角函数

    Python三角函数: 函数: ''' math.sin(x) 返回的x弧度的正弦值. math.asin(x) 返回x的反正弦弧度值. math.cos(x) 返回x的弧度的余弦值. math.ac ...

  5. PHP sizeof() 函数

    实例 返回数组中元素的数目: <?php$cars=array("Volvo","BMW","Toyota");echo sizeof ...

  6. [草稿]Skill 中的map

    https://www.cnblogs.com/yeungchie/ Skill 中的map map mapc mapcan mapcar mapcon mapinto maplist

  7. 7.9 NOI模拟赛 A.图 构造 dfs树 二分图

    啥都想不出来的我是不是废了/dk 这道题考的主要是构造 而我想的主要是乱搞. 一个很假很假的做法:直接暴力4种颜色染色 我也不知道对不对.. 不过成功的话一定是对的. 然后考虑奇环的问题 一个很假很假 ...

  8. 牛客练习赛63 C 牛牛的揠苗助长 主席树 二分 中位数

    LINK:牛牛的揠苗助长 题目很水 不过做法很多 想到一个近乎O(n)的做法 不过感觉假了 最后决定莽一个主席树 当然 平衡树也行. 容易想到 答案为ans天 那么一些点的有效增长项数为 ans%n. ...

  9. luogu P3829 [SHOI2012]信用卡凸包 凸包 点的旋转

    LINK:信用卡凸包 当 R==0的时候显然是一个点的旋转 之后再求凸包即可. 这里先说点如何旋转 如果是根据原点旋转的话 经过一个繁杂的推导可以得到一个矩阵. [cosw,-sinw] [sinw, ...

  10. [NewLife.Net]单机400万长连接压力测试

    目标 对网络库NewLife.Net进行单机百万级长连接测试,并持续收发数据,检测网络库稳定性. [2020年8月1日晚上22点] 先上源码:https://github.com/NewLifeX/N ...