Python [l1,l1.next,….l2.prev,l2]。只需返回l1,但在“取消链接”l1.prev、l2.next以及l1.prev.next和l2.next.prev之后,就可以执行此操作。然后指定l1.prev.next=l2.next和l2.next.
Python [l1,l1.next,….l2.prev,l2]。只需返回l1,但在“取消链接”l1.prev、l2.next以及l1.prev.next和l2.next.prev之后,就可以执行此操作。然后指定l1.prev.next=l2.next和l2.next.,python,linked-list,time-complexity,Python,Linked List,Time Complexity,[l1,l1.next,….l2.prev,l2]。只需返回l1,但在“取消链接”l1.prev、l2.next以及l1.prev.next和l2.next.prev之后,就可以执行此操作。然后指定l1.prev.next=l2.next和l2.next.prev=l1.prev将初始列表重新连接在一起。。。至少这是我的想法。幸运的是,我在添加(a,b,c,d)和拼接(a,c)后尝试了代码。splice(a,c)只返回a,列表(a,b,c,d)将更改为(a,b,c)Well,self。首先,或者
[l1,l1.next,….l2.prev,l2]。只需返回
l1
,但在“取消链接”l1.prev
、l2.next
以及l1.prev.next
和l2.next.prev
之后,就可以执行此操作。然后指定l1.prev.next=l2.next
和l2.next.prev=l1.prev
将初始列表重新连接在一起。。。至少这是我的想法。幸运的是,我在添加(a,b,c,d)和拼接(a,c)后尝试了代码。splice(a,c)只返回a,列表(a,b,c,d)将更改为(a,b,c)Well,self。首先
,或者无论调用什么,都需要更新以指向正确的节点。如果是那样的话,d
,或者l2。下一步
恐怕它不能正常工作。如果我理解正确,它将始终返回第一个节点,并且列表将是“应切片列表”,如果您想“返回l1和l2之间的元素”,那么您需要返回[l1,l1.next,….l2.prev,l2]
。只需返回l1
,但在“取消链接”l1.prev
、l2.next
以及l1.prev.next
和l2.next.prev
之后,就可以执行此操作。然后指定l1.prev.next=l2.next
和l2.next.prev=l1.prev
将初始列表重新连接在一起。。。至少这是我的想法。幸运的是,我在添加(a,b,c,d)和拼接(a,c)后尝试了代码。splice(a,c)只返回a,列表(a,b,c,d)将更改为(a,b,c)Well,self。首先
,或者无论调用什么,都需要更新以指向正确的节点。在这种情况下,d
,或l2。下一步
def splice(self, l1, l2):
curr = l1
s = ""
while curr.next:
s += str(curr) + "\n"
if curr is l2:
break
curr = curr.next
if l1.prev is not None:
l2.prev = l1.prev
else:
self.first = l2.next
self.first.prev = None
if l2.next is not None:
l1.next = l2.next
else:
self.last = l2.next
return s
[l1.prev]-[l1]-[l1.next]---[l2.prev]-[l2]-[l2.next]
def splice(self, l1, l2):
before, next = l1.prev, l2.next
# self.head = ... # TODO: Figure out what this should point at
if l2.next:
l2.next.prev = before if l1 else None # right-to-left
if l1.prev:
l1.prev.next = next if l2 else None # left-to-right
if l1:
l1.prev = None # detach <-l1
if l2:
l2.next = None # detach l2->
return l1 # return the sublist