题目
关键词
单链表,双链表
代码记录
方法一 单链表
class ListNode:
def __init__(self,val):
self.val=val
self.next=None
class MyLinkedList:
def __init__(self):
"""
Initialize your data structure here.
"""
self.godnode=ListNode(0)
def get(self, index: int) -> int:
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
"""
if index<0:
return -1
cur_node=self.godnode.next
cur_index=0
while True:
if not cur_node:
return -1
if index==cur_index:
return cur_node.val
else:
cur_index += 1
cur_node=cur_node.next
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.
"""
head = self.godnode.next
cur = ListNode(val)
cur.next=head
self.godnode.next=cur
def addAtTail(self, val: int) -> None:
"""
Append a node of value val to the last element of the linked list.
"""
tail=self.godnode
cur = ListNode(val)
while True:
if tail and not tail.next:
break
else:
tail = tail.next
tail.next = cur
def addAtIndex(self, index: int, val: int) -> None:
"""
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.
"""
cur_node = self.godnode
cur_index = 0
to_add = ListNode(val)
add_flag=True
index = max(0,index)
while True:
if cur_index==index:
break
else:
cur_index += 1
if not cur_node:
add_flag=False
break
else:
cur_node = cur_node.next
if add_flag:
next_node = cur_node.next
to_add.next = next_node
cur_node.next = to_add
def deleteAtIndex(self, index: int) -> None:
"""
Delete the index-th node in the linked list, if the index is valid.
"""
pre=self.godnode
cur_index=0
del_flag=True
while True:
if cur_index==index:
break
else:
cur_index +=1
if not pre or not pre.next or not pre.next.next:
del_flag=False
break
else:
pre = pre.next
if del_flag:
if pre and pre.next:
pre.next = pre.next.next
def print_cur_list(self):
root=self.godnode.next
print('current list:')
while root:
print(str(root.val)+' ')
root=root.next
方法二 双链表
class ListNode:
def __init__(self,val):
self.val=val
self.next=None
self.prev=None
class MyLinkedList:
def __init__(self):
"""
Initialize your data structure here.
"""
self.godnode=ListNode(0)
def get(self, index: int) -> int:
"""
Get the value of the index-th node in the linked list. If the index is invalid, return -1.
"""
if index<0:
return -1
cur_node=self.godnode.next
while cur_node:
if index==0:
return cur_node.val
else:
cur_node=cur_node.next
index -=1
return -1
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.
"""
head=self.godnode.next
cur_node=ListNode(val)
cur_node.next=head
if head:
head.prev=cur_node
self.godnode.next=cur_node
cur_node.prev=self.godnode
def addAtTail(self, val: int) -> None:
"""
Append a node of value val to the last element of the linked list.
"""
tail=self.godnode
cur_node=ListNode(val)
while tail and tail.next:
tail=tail.next
tail.next=cur_node
cur_node.prev=tail
def addAtIndex(self, index: int, val: int) -> None:
"""
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.
"""
if index<0:
return
cur_node=ListNode(val)
cur=self.godnode
while cur and cur.next:
if index==0:
break
else:
cur=cur.next
index-=1
if index==0:
prev_node=cur
next_node=cur.next
prev_node.next=cur_node
cur_node.next=next_node
cur_node.prev=prev_node
if next_node:
next_node.prev=cur_node
def deleteAtIndex(self, index: int) -> None:
"""
Delete the index-th node in the linked list, if the index is valid.
"""
if index<0:
return
cur=self.godnode.next
while cur and cur.next:
if index==0:
break
else:
cur=cur.next
index-=1
if index==0:
if cur:
prev_node=cur.prev
next_node=cur.next
prev_node.next=next_node
if next_node:
next_node.prev=prev_node
def print_cur_list(self):
root=self.godnode.next
print('current list:')
while root:
print(str(root.val)+' ')
root=root.next