Recca Chao 的 gitHub page

推廣網站開發,包含 Laravel 和 Kotlin 後端撰寫、自動化測試、讀書心得等。Taiwan Kotlin User Group 管理員。

View on GitHub

Hi, here’s your problem today. This problem was recently asked by Twitter:

You are given an array of k sorted singly linked lists. Merge the linked lists into a single sorted linked list and return it.

Here’s your starting point:

class Node(object):
 def __init__(self, val, next=None):
   self.val = val
   self.next = next

 def __str__(self):
   c = self
   answer = ""
   while c:
     answer += str(c.val) if c.val else ""
     c = c.next
   return answer

def merge(lists):
 # Fill this in.

a = Node(1, Node(3, Node(5)))
b = Node(2, Node(4, Node(6)))
print merge([a, b])
# 123456