39/300
- 设计链表
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性: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 库。
测试集:
input:
[“MyLinkedList”,“addAtHead”,“addAtTail”,“addAtIndex”,“get”,“deleteAtIndex”,“get”]
[[],[1],[3],[1,2],[1],[1],[1]]
output:[null,null,null,null,2,null,3]
input:
[“MyLinkedList”,“addAtIndex”,“get”,“deleteAtIndex”]
[[],[-1,0],[0],[-1]]
output:[null,null,0,null]
input:
[“MyLinkedList”,“addAtHead”,“addAtTail”,“addAtIndex”,“get”,“deleteAtIndex”,“get”]
[[],[1],[3],[1,2],[-1],[1],[-3]]
output:[null,null,null,null,-1,null,-1]
特别基础且烦人的题,小问题一堆,试了好几次才ok。继续考验了找bug的能力!
class Node(object):
def __init__(self,x):
self.val = x
self.next = None
class MyLinkedList(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.head = None
self.count = 0
def get(self, index):
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
:type index: int
:rtype: int
"""
if index < 0 or index >= self.count:
return -1
if not self.head:
return -1
cur = self.head
for i in range(index):
cur = cur.next
return cur.val
def addAtHead(self, 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.
:type val: int
:rtype: None
"""
node = Node(val)
node.next = self.head
self.head = node
self.count += 1
def addAtTail(self, val):
"""
Append a node of value val to the last element of the linked list.
:type val: int
:rtype: None
"""
node = Node(val)
if not self.head:
return self.addAtHead(val)
cur = self.head
while cur.next:
cur = cur.next
node.next = cur.next
cur.next = node
self.count += 1
def addAtIndex(self, 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.
:type index: int
:type val: int
:rtype: None
"""
if index > self.count:
return
elif index <= 0:
return self.addAtHead(val)
elif index == self.count:
return self.addAtTail(val)
else:
node = Node(val)
cur = self.head
for i in range(index-1):
cur = cur.next
node.next = cur.next
cur.next = node
self.count += 1
def deleteAtIndex(self, index):
"""
Delete the index-th node in the linked list, if the index is valid.
:type index: int
:rtype: None
"""
if index < 0 or index >= self.count: return
cur = self.head
if index == 0: self.head = cur.next
else:
for i in range(index -1):
cur = cur.next
cur.next = cur.next.next
self.count -= 1