diff --git "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" index 5c78a12a7f..c72b73277a 100644 --- "a/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" +++ "b/problems/0707.\350\256\276\350\256\241\351\223\276\350\241\250.md" @@ -486,15 +486,10 @@ class MyLinkedList { Python: ```python # 单链表 -class Node(object): - def __init__(self, x=0): - self.val = x - self.next = None - -class MyLinkedList(object): +class MyLinkedList1: def __init__(self): - self.head = Node() + self.dummy_head = Node()# 添加虚拟头指针,便于操作 self.size = 0 # 设置一个链表长度的属性,便于后续操作,注意每次增和删的时候都要更新 def get(self, index): @@ -504,7 +499,7 @@ class MyLinkedList(object): """ if index < 0 or index >= self.size: return -1 - cur = self.head.next + cur = self.dummy_head.next while(index): cur = cur.next index -= 1 @@ -516,8 +511,8 @@ class MyLinkedList(object): :rtype: None """ new_node = Node(val) - new_node.next = self.head.next - self.head.next = new_node + new_node.next = self.dummy_head.next + self.dummy_head.next = new_node self.size += 1 def addAtTail(self, val): @@ -526,7 +521,7 @@ class MyLinkedList(object): :rtype: None """ new_node = Node(val) - cur = self.head + cur = self.dummy_head while(cur.next): cur = cur.next cur.next = new_node @@ -548,12 +543,12 @@ class MyLinkedList(object): return node = Node(val) - pre = self.head + cur = self.dummy_head while(index): - pre = pre.next + cur = cur.next index -= 1 - node.next = pre.next - pre.next = node + node.next = cur.next + cur.next = node self.size += 1 def deleteAtIndex(self, index): @@ -563,7 +558,7 @@ class MyLinkedList(object): """ if index < 0 or index >= self.size: return - pre = self.head + pre = self.dummy_head while(index): pre = pre.next index -= 1 @@ -574,11 +569,10 @@ class MyLinkedList(object): # 相对于单链表, Node新增了prev属性 class Node: - def __init__(self, val): + def __init__(self, val=0, next = None, prev = None): self.val = val - self.prev = None - self.next = None - + self.next = next + self.prev = prev class MyLinkedList: @@ -601,6 +595,20 @@ class MyLinkedList: node = node.next return node + + def _update(self, prev: Node, next: Node, val: int) -> None: + """ + 更新节点 + :param prev: 相对于更新的前一个节点 + :param next: 相对于更新的后一个节点 + :param val: 要添加的节点值 + """ + # 计数累加 + self._count += 1 + node = Node(val) + prev.next, next.prev = node, node + node.prev, node.next = prev, next + 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. @@ -634,19 +642,6 @@ class MyLinkedList: node = self._get_node(index) self._update(node.prev, node, val) - def _update(self, prev: Node, next: Node, val: int) -> None: - """ - 更新节点 - :param prev: 相对于更新的前一个节点 - :param next: 相对于更新的后一个节点 - :param val: 要添加的节点值 - """ - # 计数累加 - self._count += 1 - node = Node(val) - prev.next, next.prev = node, node - node.prev, node.next = prev, next - def deleteAtIndex(self, index: int) -> None: """ Delete the index-th node in the linked list, if the index is valid. @@ -656,6 +651,16 @@ class MyLinkedList: # 计数-1 self._count -= 1 node.prev.next, node.next.prev = node.next, node.prev + + + +# Your MyLinkedList object will be instantiated and called as such: +# obj = MyLinkedList() +# param_1 = obj.get(index) +# obj.addAtHead(val) +# obj.addAtTail(val) +# obj.addAtIndex(index,val) +# obj.deleteAtIndex(index) ``` Go: