页面顶部 Top
文件:  root - text - article - 2019 - 10 - daily-problem-add-two-numbers-as-a-linked-list.txt
标签: 每天算法, 算法, 数据结构, 链表, daily problem, linked list, programming, algorithm, | 英文 | 主页 | 类别: 计算机科学 | 238 次阅读, 22530 次搜索 | 214 个单词

定阅此目录的博客 | 浏览 | 博客存档
This problem was recently asked by Microsoft: You are given two linked-lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
Here is the function signature as a starting point (in Python):


# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None

class Solution:
def addTwoNumbers(self, l1, l2, c = 0):
# Fill this in.

l1 = ListNode(2)
l1.next = ListNode(4)
l1.next.next = ListNode(3)

l2 = ListNode(5)
l2.next = ListNode(6)
l2.next.next = ListNode(4)

result = Solution().addTwoNumbers(l1, l2)
while result:
print result.val,
result = result.next
# 7 0 8


Why Python? We recommend using Python as a generalist language for interviewing, as it is well-regarded in the tech industry and used across Google/YouTube, Facebook/Instagram, Netflix, Uber, Dropbox, Pinterest, Spotify, etc., It is easy to learn with readable syntax, and very similar in structure to other popular languages like Java, C/C++, Javascript, PHP, Ruby, etc. Python is generally faster to read/write though, which makes it ideal for interviews. You can, of course, use any language you like!
标签: 每天算法, 算法, 数据结构, 链表, daily problem, linked list, programming, algorithm, | 英文 | 主页 | 类别: 计算机科学 | 238 次阅读, 22530 次搜索 | 214 个单词 定阅此目录的博客

猜您喜欢...

  1. Daily Interview Problem:Create a balanced binary search tree
  2. Daily Interview Problem: Reverse Integer
  3. Daily Interview Question: Longest Sequence with Two Unique Numbers
  4. Algorithm Interview: String Compression
  5. [Daily Problem] Course Prerequisites
  6. Daily Interview Problem: Count Number of Unival Subtrees
  7. Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
  8. Daily Interview Problem: Room scheduling
  9. Top K Frequent words
  10. [Daily Problem] Longest Palindromic Substring

评论 (0)

您的邮件 (不公开域名部分):

您的评论:

悄悄话(发邮件) 颜色 更多笑脸 S x y @

验证码 (点击图片更新):

    当前页暂时没有评论。


最后更新: October 30 2020 14:21:12 | RSS Subscription
牛排怎么做才好吃? | <meta name="机器人" content="不索引, 跟踪" />