# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
a = ListNode(5)
b = ListNode(10)
c = ListNode(20)
e = ListNode(0)
f = ListNode(5)
g = ListNode(21)
h = ListNode(30)
a.next = b
b.next = c
e.next = f
f.next = g
g.next = h
因此,我有两个头为a
和e
的单链接列表
我想按其值的升序合并。现在,我要合并它们,以遍历两个链表,比较值,直到其中一个链表到达None
(其中一个链表比另一个链表短,所以一个将到达{{1} }之前)
None
因此,这是我理想中要发生的事情,但显然不会发生,正如您在打印报表时所看到的!
class Solution:
def mergeTwoLists(self, l1, l2):
tempNode = ListNode(0)
returnNode = tempNode
while (l1.next != None) and (l2.next != None):
if l1.val < l2.val:
print("l1.val < l2.val", l1.val, l2.val)
tempNode.next = l1
tempNode = tempNode.next
l1 = l1.next
elif l1.val == l2.val:
print("l1.val == l2.val", l1.val, l2.val)
#If both the values are equal, assign l1's value first
#then make l2's value follow l1's value using tempNode
tempNode.next = l1
tempNode = tempNode.next #Because of the previous statement, after execution of this statement, I'm assuming tempNode.val is now l1.val and tempNode.next = l1.next
#tempNode.next is supposed to be equal to l1.next, instead assign it to l2
tempNode.next = l2
tempNode = tempNode.next #Because of the previous statement, after execution of this statement, I'm assuming tempNode.val is now l2.val and tempNode.next = l2.next
#Increment both l1 and l2
l1 = l1.next
l2 = l2.next
else:
print("l1.val > l2.val", l1.val, l2.val)
tempNode.next = l2
tempNode = tempNode.next
l2 = l2.next
sol = Solution()
sol.mergeTwoLists(a, e)
增加或前进l2!
l1.val = 5 > l2.val = 0
,即l1.val = 5
==
它们都相等,因此将l2.val == 5
移至下一个,并将l1
移至下一个
现在,l2
应该是l1.val
,而10
应该是l2.val
,但是
21
被打印为l1.val
,而5
被打印为l2.val
并陷入无限循环。
为什么21
停留在l1.val
而不是更新为5
,为什么它停留在这个无限循环中而不是其中一个依我的{{1 }}语句?
答案 0 :(得分:2)
问题在下面的代码片段中
tempNode.next = l1
tempNode = tempNode.next
tempNode.next = l2
tempNode = tempNode.next
l1 = l1.next
l2 = l2.next
只需将其更改为以下内容,您的代码即可使用
tempNode.next = l1
tempNode = tempNode.next
l1 = l1.next
tempNode.next = l2
tempNode = tempNode.next
l2 = l2.next
您的方法存在的问题是,当您执行tempNode.next = l2
时,您正在修改ListNode
所指向的实际l1
,这会使您陷入无限循环之中
答案 1 :(得分:1)
您需要将l1
和l2
的值分配给tempNode.val
,而不是将l1
和l2
节点本身分配给tempNode
'的下一个节点。如果其他列表为空,则还需要将l1
或l2
的剩余值添加到tempNode
中。
# Definition for singly-linked list.
class ListNode:
def __init__(self, x=None):
self.val = x
self.next = None
a = ListNode(5)
b = ListNode(10)
c = ListNode(20)
e = ListNode(0)
f = ListNode(5)
g = ListNode(21)
h = ListNode(30)
a.next = b
b.next = c
e.next = f
f.next = g
g.next = h
class Solution:
def mergeTwoLists(self, l1, l2):
returnNode = tempNode = ListNode()
while l1 or l2:
if not l1:
print('l1 is empty; adding value from l2:', l2.val)
tempNode.val = l2.val
tempNode.next = ListNode()
tempNode = tempNode.next
l2 = l2.next
elif not l2:
print('l2 is empty; adding value from l1:', l1.val)
tempNode.val = l1.val
tempNode.next = ListNode()
tempNode = tempNode.next
l1 = l1.next
elif l1.val < l2.val:
print("l1.val < l2.val", l1.val, l2.val)
tempNode.val = l1.val
tempNode.next = ListNode()
tempNode = tempNode.next
l1 = l1.next
elif l1.val == l2.val:
print("l1.val == l2.val", l1.val, l2.val)
#If both the values are equal, assign l1's value first
#then make l2's value follow l1's value using tempNode
tempNode.val = l1.val
tempNode.next = ListNode() #Because of the previous statement, after execution of this statement, I'm assuming tempNode.val is now l1.val and tempNode.next = l1.next
tempNode = tempNode.next
#tempNode.next is supposed to be equal to l1.next, instead assign it to l2
tempNode.val = l2.val
tempNode.next = ListNode() #Because of the previous statement, after execution of this statement, I'm assuming tempNode.val is now l2.val and tempNode.next = l2.next
tempNode = tempNode.next
#Increment both l1 and l2
l1 = l1.next
l2 = l2.next
else:
print("l1.val > l2.val", l1.val, l2.val)
tempNode.val = l2.val
tempNode.next = ListNode()
tempNode = tempNode.next
l2 = l2.next
return returnNode
sol = Solution()
node = sol.mergeTwoLists(a, e)
while node and node.val is not None:
print(node.val)
node = node.next
这将输出:
l1.val > l2.val 5 0
l1.val == l2.val 5 5
l1.val < l2.val 10 21
l1.val < l2.val 20 21
l1 is empty; adding value from l2: 21
l1 is empty; adding value from l2: 30
0
5
5
10
20
21
30