Skip to content

Commit

Permalink
Update 0707.设计链表.md
Browse files Browse the repository at this point in the history
修改 python 代码,dummy node
  • Loading branch information
jianghongcheng authored Apr 20, 2023
1 parent f848b4f commit f7bdc58
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions problems/0707.设计链表.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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:

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit f7bdc58

Please sign in to comment.