【发布时间】:2019-12-13 13:55:28
【问题描述】:
我实现了以下代码,它可以工作,但它在链表末尾添加了新值,例如 [1,2,3,6],值 4 结果是 [1,2,3,6,4] 这是错误的 正确的结果是[1,2,3,4,6]
# Singly-linked lists are already defined with this interface:
class ListNode(object):
def __init__(self, x):
self.value = x
self.next = None
def insertValueIntoSortedLinkedList(head, valuetoInsert):
currentNode = head
while currentNode is not None:
if currentNode.next is None:
currentNode.next = ListNode(valuetoInsert)
return head
currentNode = currentNode.next
我的问题如何修改 insertValueIntoSortedLinkedList 函数 谢谢
【问题讨论】: