To the Top
File:  root - text - article - 2019 - 11 - merge-k-sorted-linked-list.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 189 Views, 16478 Search Bots | 98 Words

Subscribe to Feed Burner | Browse | Archive
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
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 189 Views, 16478 Search Bots | 98 Words Subscribe to Feed Burner

Related Articles

  1. Daily Interview Problem: Get all Values at a Certain Height in a Binary Tree
  2. Daily Interview Question: Longest Sequence with Two Unique Numbers
  3. Absolute Path
  4. Daily Interview Problem: Maximum Profit From Stocks
  5. Batch Programming in XP
  6. Algorithm Interview: Permutations of numbers
  7. Algorithm Interview: Make the Largest Number
  8. [Daily Problem] Longest Palindromic Substring
  9. Algorithm Interview: Lowest Common Ancestor of 2 Nodes in Binary Tree
  10. [Daily Problem] Add two numbers as a linked list

Comments (0)

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">