Skip to content

Instantly share code, notes, and snippets.

@nabeen
Created May 11, 2020 15:25
Show Gist options
  • Save nabeen/4746fbd143ab364f7ecfe4e39fe68b7e to your computer and use it in GitHub Desktop.
Save nabeen/4746fbd143ab364f7ecfe4e39fe68b7e to your computer and use it in GitHub Desktop.
add-two-numbers.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "add-two-numbers.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyPPx4i2XyOdBbbGqcldITGE",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/nabeen/4746fbd143ab364f7ecfe4e39fe68b7e/add-two-numbers.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pSb6-H3i_Jul",
"colab_type": "code",
"colab": {}
},
"source": [
"class ListNode:\n",
" def __init__(self, val=0, next=None):\n",
" self.val = val\n",
" self.next = next\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Y8V-s2nJGUAn",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
},
"outputId": "5db64af6-e400-40a9-9850-ec0a0af83b4f"
},
"source": [
"class Solution:\n",
" def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n",
" while l1 is not None or l2 is not None:\n",
" print(\"l1の値:{l1}\".format(l1=l1.val))\n",
" print(\"l2の値:{l2}\".format(l2=l2.val))\n",
" l1 = l1.next\n",
" l2 = l2.next\n",
"\n",
"\n",
"l1 = ListNode(2, ListNode(4, ListNode(3, None)))\n",
"l2 = ListNode(5, ListNode(6, ListNode(4, None)))\n",
"sl = Solution()\n",
"ret = sl.addTwoNumbers(l1, l2)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"l1の値:2\n",
"l2の値:5\n",
"l1の値:4\n",
"l2の値:6\n",
"l1の値:3\n",
"l2の値:4\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "M_Eth8S9M_2F",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 170
},
"outputId": "16c8f58b-d8b1-487a-b78b-1c6b47ab7851"
},
"source": [
"class Solution:\n",
" def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n",
" ans = ListNode(0)\n",
" node = ans\n",
" while l1 is not None or l2 is not None:\n",
" print(\"l1の値:{l1}\".format(l1=l1.val))\n",
" print(\"l2の値:{l2}\".format(l2=l2.val))\n",
" x = l1.val\n",
" y = l2.val\n",
"\n",
" node.next = ListNode(x + y)\n",
" node = node.next\n",
"\n",
" l1 = l1.next\n",
" l2 = l2.next\n",
" \n",
" return ans.next\n",
"\n",
"\n",
"l1 = ListNode(2, ListNode(4, ListNode(3, None)))\n",
"l2 = ListNode(5, ListNode(6, ListNode(4, None)))\n",
"sl = Solution()\n",
"ret = sl.addTwoNumbers(l1, l2)\n",
"while ret.next is not None:\n",
" print(ret.val)\n",
" ret = ret.next\n",
" if ret.next is None:\n",
" print(ret.val)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"l1の値:2\n",
"l2の値:5\n",
"l1の値:4\n",
"l2の値:6\n",
"l1の値:3\n",
"l2の値:4\n",
"7\n",
"10\n",
"7\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "oYvTayShJSyI",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 68
},
"outputId": "43bcd7a8-c9f5-4792-da54-25bf61692280"
},
"source": [
"class Solution:\n",
" def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:\n",
" ans = ListNode(0)\n",
" node = ans\n",
" carry = 0\n",
" while l1 is not None or l2 is not None:\n",
" x = l1.val if l1 is not None else 0\n",
" y = l2.val if l2 is not None else 0\n",
" \n",
" total = (carry + x + y) % 10\n",
" carry = (carry + x + y) // 10\n",
" \n",
" node.next = ListNode(total)\n",
" node = node.next\n",
" \n",
" if l1 is not None:\n",
" l1 = l1.next\n",
" if l2 is not None:\n",
" l2 = l2.next\n",
" if(carry > 0):\n",
" node.next = ListNode(carry)\n",
"\n",
" return ans.next\n",
"\n",
"l1 = ListNode(2, ListNode(4, ListNode(3, None)))\n",
"l2 = ListNode(5, ListNode(6, ListNode(4, None)))\n",
"sl = Solution()\n",
"ret = sl.addTwoNumbers(l1, l2)\n",
"while ret.next is not None:\n",
" print(ret.val)\n",
" ret = ret.next\n",
" if ret.next is None:\n",
" print(ret.val)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"7\n",
"0\n",
"8\n"
],
"name": "stdout"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment