Merge two sorted linked lists and return it as a new list. The new
list should be made by splicing together the nodes of the first two
lists.
Example 1: Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
Approach 1: dummy list
This is a straightforward method for our problem. Setting two linked
list newNode and out with the same memory location. Then use them to
note the merged list.
# Definition for singly-linked list. classListNode: def__init__(self, x): self.val = x self.next = None
defstringToListNode(input): # Now convert that list into linked list dummyRoot = ListNode(0) ptr = dummyRoot for number ininput: ptr.next = ListNode(number) ptr = ptr.next ptr = dummyRoot.next return ptr
deflistNodeToString(node): ifnot node: return"[]" result = "" while node: result += str(node.val) + ", " node = node.next return"[" + result[:-2] + "]"# 最后一个逗号不输出