标签:and 双向 end delete 应该 operation fun 删除链表 返回
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:
index
-th node in the linked list. If the index is invalid, return -1
.val
before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.val
to the last element of the linked list.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.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:
[1, 1000]
.[1, 1000]
.设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val
和 next
。val
是当前节点的值,next
是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev
以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
index
个节点的值。如果索引无效,则返回-1
。val
的节点。插入后,新节点将成为链表的第一个节点。val
的节点追加到链表的最后一个元素。index
个节点之前添加值为 val
的节点。如果 index
等于链表的长度,则该节点将附加到链表的末尾。如果 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]
之内。
注意·的点:空链表,索引范围为0~超出链表长度~我改了两次才通过。
1 class ListNode: # 定义结点类型 2 def __init__(self, x): 3 self.val = x 4 self.next = None 5 6 class MyLinkedList: 7 8 def __init__(self): 9 """ 10 Initialize your data structure here. 11 """ 12 self.head = None # 初始化一个头结点为空 13 14 15 def get(self, index: int) -> int: # 返回第index个结点,若index不合法则返回-1 16 """ 17 Get the value of the index-th node in the linked list. If the index is invalid, return -1. 18 """ 19 if self.head == None: # 空链表对于所有index都不合法 20 return -1 21 p = self.head 22 for i in range(index): 23 if not p.next: 24 return -1 25 p = p.next 26 return p.val 27 28 29 def addAtHead(self, val: int) -> None: # 在头部插入一个结点 30 """ 31 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. 32 """ 33 if self.head == None: # 对空链表直接将该结点赋予头结点即可 34 self.head = ListNode(val) 35 else: 36 new_node = ListNode(val) 37 new_node.next = self.head 38 self.head = new_node # 更新头结点 39 40 41 def addAtTail(self, val: int) -> None: # 在尾部插入一个结点 42 """ 43 Append a node of value val to the last element of the linked list. 44 """ 45 if self.head == None: # 对空链表直接将该结点赋予头结点即可 46 self.head = ListNode(val) 47 p = self.head 48 while(p.next): 49 p = p.next 50 p.next = ListNode(val) 51 52 53 54 def addAtIndex(self, index: int, val: int) -> None: # 在第index位置插入结点 55 """ 56 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. 57 """ 58 p = self.head 59 new_node = ListNode(val) 60 if not self.head: # 若为空链表,除非index=0,才将其作为头结点,否则一切index都不合法 61 if index==0: 62 new_node.next = self.head 63 self.head = new_node 64 return None 65 for i in range(index-1): 66 if not p.next: 67 return None 68 p = p.next 69 new_node.next = p.next 70 p.next = new_node 71 72 73 def deleteAtIndex(self, index: int) -> None: # 删除index位置的结点 74 """ 75 Delete the index-th node in the linked list, if the index is valid. 76 """ 77 if not self.head: # 空链表退出 78 return None 79 if index == 0: # 头结点单独考虑,直接将头结点赋予下一个结点即可 80 self.head = self.head.next 81 return None 82 p = self.head 83 for i in range(index-1): 84 if not p.next: 85 return None 86 p = p.next 87 if p.next: 88 p.next = p.next.next
验证之前可以写一个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
标签:and 双向 end delete 应该 operation fun 删除链表 返回
原文地址:https://www.cnblogs.com/king-lps/p/10658150.html