[21] Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/description/
- algorithms
- Easy (44.15%)
- Source Code: 21.merge-two-sorted-lists.py
- Total Accepted: 553.4K
- Total Submissions: 1.2M
- Testcase Example: '[1,2,4]\n[1,3,4]'
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:
Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4
python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
l1_values, l2_values = [], []
while l1:
l1_values.append(l1.val)
l1 = l1.next
while l2:
l2_values.append(l2.val)
l2 = l2.next
l1_values.extend(l2_values)
l1_values.sort()
len1 = len(l1_values)
record, head = None, None
for i in l1_values[::-1]:
head = ListNode(i)
head.next = record
record = head
return head