1. 原始题目

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 nextval 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.

Example:

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.

2. 题目理解

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 nextval 是当前节点的值,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 库。

注意·的点:空链表,索引范围为0~超出链表长度~我改了两次才通过。

3. 解题

 class ListNode:          # 定义结点类型
def __init__(self, x):
self.val = x
self.next = None class MyLinkedList: def __init__(self):
"""
Initialize your data structure here.
"""
self.head = None # 初始化一个头结点为空 def get(self, index: int) -> int: # 返回第index个结点,若index不合法则返回-1
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
"""
if self.head == None: # 空链表对于所有index都不合法
return -1
p = self.head
for i in range(index):
if not p.next:
return -1
p = p.next
return p.val def addAtHead(self, val: int) -> None: # 在头部插入一个结点
"""
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.
"""
if self.head == None: # 对空链表直接将该结点赋予头结点即可
self.head = ListNode(val)
else:
new_node = ListNode(val)
new_node.next = self.head
self.head = new_node # 更新头结点 def addAtTail(self, val: int) -> None: # 在尾部插入一个结点
"""
Append a node of value val to the last element of the linked list.
"""
if self.head == None: # 对空链表直接将该结点赋予头结点即可
self.head = ListNode(val)
p = self.head
while(p.next):
p = p.next
p.next = ListNode(val) def addAtIndex(self, index: int, val: int) -> None: # 在第index位置插入结点
"""
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.
"""
p = self.head
new_node = ListNode(val)
if not self.head: # 若为空链表,除非index=0,才将其作为头结点,否则一切index都不合法
if index==0:
new_node.next = self.head
self.head = new_node
return None
for i in range(index-1):
if not p.next:
return None
p = p.next
new_node.next = p.next
p.next = new_node def deleteAtIndex(self, index: int) -> None: # 删除index位置的结点
"""
Delete the index-th node in the linked list, if the index is valid.
"""
if not self.head: # 空链表退出
return None
if index == 0: # 头结点单独考虑,直接将头结点赋予下一个结点即可
self.head = self.head.next
return None
p = self.head
for i in range(index-1):
if not p.next:
return None
p = p.next
if p.next:
p.next = p.next.next

4. 验证

验证之前可以写一个print函数打印当前链表情况:写到类里面去

    def printlist(self):
p = self.head
while(p):
print(p.val,end=' ')
p = p.next
print('\n')

测试:

linkedList = MyLinkedList()

linkedList.addAtHead(5)
linkedList.printlist() linkedList.addAtHead(2)
linkedList.printlist() linkedList.deleteAtIndex(1)
linkedList.printlist() linkedList.addAtIndex(1,9)
linkedList.printlist() linkedList.addAtHead(4)
linkedList.printlist() linkedList.addAtHead(9)
linkedList.printlist() linkedList.addAtHead(8)
linkedList.printlist() print(linkedList.get(3) ) linkedList.addAtTail(1)
linkedList.printlist() linkedList.addAtIndex(3,6)
linkedList.printlist() linkedList.addAtHead(3)
linkedList.printlist()

5

2 5

2

2 9

4 2 9

9 4 2 9

8 9 4 2 9

2
8 9 4 2 9 1

8 9 4 6 2 9 1

3 8 9 4 6 2 9 1

707. Design Linked List的更多相关文章

  1. 【Leetcode_easy】707. Design Linked List

    problem 707. Design Linked List 参考 1. Leetcode_easy_707. Design Linked List; 完

  2. #Leetcode# 707. Design Linked List

    https://leetcode.com/problems/design-linked-list/ Design your implementation of the linked list. You ...

  3. 【LeetCode】707. Design Linked List 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode 707. Design Linked List (设计链表)

    题目标签:Linked List 题目让我们自己设计一个 linked list,可以是单向和双向的.这里选的是单向,题目并不是很难,但要考虑到所有的情况,具体看code. Java Solution ...

  5. [LeetCode] Design Linked List 设计链表

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

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

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

  7. 【LeetCode】Design Linked List(设计链表)

    这道题是LeetCode里的第707到题.这是在学习链表时碰见的. 题目要求: 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的 ...

  8. LeetCode707:设计链表 Design Linked List

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

  9. Design Linked List

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

随机推荐

  1. Mysql连接查询、子查询、联合查询 整理

    连接查询 连接语法的基本形式 from 表1  [连接方式]  join 表2  [on 连接条件]: 交叉连接 语法: from  表1  [cross]  join  表2  ;      //可 ...

  2. 基于jieba,TfidfVectorizer,LogisticRegression进行搜狐新闻文本分类

    一.简介 此文是对利用jieba,word2vec,LR进行搜狐新闻文本分类的准确性的提升,数据集和分词过程一样,这里就不在叙述,读者可参考前面的处理过程 经过jieba分词,产生24000条分词结果 ...

  3. Spring JPA学习笔记

    目录 什么是JPA? 引入配置 新建一个Entity Bean类 JPA的增删改查 新建操作接口 新建测试类 总结 什么是JPA? 什么是JDBC知道吧?数据库有Mysql,SQL Server,Or ...

  4. Part-Four

    1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

  5. [Android] Android 注解绑定UI View组件库 ButterKnife 的使用

    ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象,有了ButterKnife可以很轻松的省去这些步骤.是大神JakeW ...

  6. Photoshop影像匀色技术

    本篇博文简单介绍一下利用PhotoShop对影像数据进行匀色的相关技术 影像一般有img和tif两种各种.一般的影像如果在PS中打开,会丢失坐标信息.在做匀色处理中,普通的做法是,先将坐标信息导出来, ...

  7. IScroll5要防止重复加载

    增加一个判断条件,ajax未返回前,设置为true,返回前设置为false,只有为false下才能够出发加载数据事件效果好很多.

  8. C# WinForm多线程(一)----- Thread类库

    Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括 ...

  9. MVC数据库连接

    创建数据库 创建表 来源:http://blog.csdn.net/tkdwave520/article/details/44629903 <pre  name = “code”  class ...

  10. 【python小练】0012题

    第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好 ...