{"id":"add-two-numbers","title":"Add Two Numbers","difficulty":"medium","patterns":["linked-list"],"primaryConcept":"linked-list-reversal","concepts":["linked-list-reversal"],"problemForm":"core","companies":[],"category":"trees-graphs","compareMode":"exact","syntheticTestConfig":{"inputShape":"two-linked-lists","primaryParam":"l1","secondaryParam":"l2","sizeMetric":"node-count"},"description":"Two non-empty linked lists store non-negative integers in reverse digit order. Add the numbers and return the sum as a linked list in the same reverse order.","examples":[{"input":"l1 = [2,4,3], l2 = [5,6,4]","output":"[7,0,8]","explanation":"342 + 465 = 807, so the reverse-order result is [7,0,8]."},{"input":"l1 = [0], l2 = [0]","output":"[0]","explanation":"Zero plus zero is zero."},{"input":"l1 = [9,9,9,9], l2 = [9,9]","output":"[8,9,0,0,1]","explanation":"9999 + 99 = 10098, stored in reverse order."}],"constraints":[{"id":"1-length-of-each-list-100","text":"1 <= length of each list <= 100","check":[{"kind":"sizeRange","target":"l1","measure":"listNodes","min":1,"max":100},{"kind":"sizeRange","target":"l2","measure":"listNodes","min":1,"max":100}]},{"id":"0-node-val-9","text":"0 <= Node.val <= 9","check":[{"kind":"valueRange","target":"l1","selector":"listValues","min":0,"max":9},{"kind":"valueRange","target":"l2","selector":"listValues","min":0,"max":9}]},{"id":"each-list-represents-a-number-in-reverse-digit-order","text":"Each list represents a number in reverse digit order"},{"id":"the-lists-do-not-contain-leading-zeroes-except-for-the-number-0-","text":"The lists do not contain leading zeroes except for the number 0 itself"}],"starterCode":"# TraceCode provides ListNode at runtime.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\n\nclass Solution:\n    def addTwoNumbers(self, l1: ListNode | None, l2: ListNode | None) -> ListNode | None:\n        # Your code here\n        pass","functionName":"addTwoNumbers","testCases":[{"id":"1","input":{"l1":{"__type__":"ListNode","__id__":"l1-0","val":2,"next":{"__type__":"ListNode","__id__":"l1-1","val":4,"next":{"__type__":"ListNode","__id__":"l1-2","val":3,"next":null}}},"l2":{"__type__":"ListNode","__id__":"l2-0","val":5,"next":{"__type__":"ListNode","__id__":"l2-1","val":6,"next":{"__type__":"ListNode","__id__":"l2-2","val":4,"next":null}}}},"expected":{"__type__":"ListNode","val":7,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":8,"next":null}}},"description":"Canonical example with an internal carry."},{"id":"2","input":{"l1":{"__type__":"ListNode","__id__":"l1-10","val":0,"next":null},"l2":{"__type__":"ListNode","__id__":"l2-10","val":0,"next":null}},"expected":{"__type__":"ListNode","val":0,"next":null},"description":"Both numbers are zero."},{"id":"3","input":{"l1":{"__type__":"ListNode","__id__":"l1-20","val":9,"next":{"__type__":"ListNode","__id__":"l1-21","val":9,"next":{"__type__":"ListNode","__id__":"l1-22","val":9,"next":{"__type__":"ListNode","__id__":"l1-23","val":9,"next":null}}}},"l2":{"__type__":"ListNode","__id__":"l2-20","val":9,"next":{"__type__":"ListNode","__id__":"l2-21","val":9,"next":null}}},"expected":{"__type__":"ListNode","val":8,"next":{"__type__":"ListNode","val":9,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":1,"next":null}}}}},"description":"Carry continues after the shorter list ends."},{"id":"4","input":{"l1":{"__type__":"ListNode","__id__":"l1-30","val":5,"next":null},"l2":{"__type__":"ListNode","__id__":"l2-30","val":5,"next":null}},"expected":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":1,"next":null}},"description":"Final carry creates one extra node."},{"id":"h1","input":{"l1":{"__type__":"ListNode","__id__":"l1-h1-0","val":9,"next":null},"l2":{"__type__":"ListNode","__id__":"l2-h1-0","val":1,"next":null}},"expected":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":1,"next":null}},"description":"A single carry creates a new most-significant digit."},{"id":"h2","input":{"l1":{"__type__":"ListNode","__id__":"l1-h2-0","val":0,"next":{"__type__":"ListNode","__id__":"l1-h2-1","val":1,"next":null}},"l2":{"__type__":"ListNode","__id__":"l2-h2-0","val":0,"next":{"__type__":"ListNode","__id__":"l2-h2-1","val":9,"next":null}}},"expected":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":1,"next":null}}},"description":"Unequal lengths still propagate carry through the longer tail."},{"id":"h3","input":{"l1":{"__type__":"ListNode","__id__":"l1-h3-0","val":2,"next":{"__type__":"ListNode","__id__":"l1-h3-1","val":4,"next":{"__type__":"ListNode","__id__":"l1-h3-2","val":3,"next":null}}},"l2":{"__type__":"ListNode","__id__":"l2-h3-0","val":5,"next":{"__type__":"ListNode","__id__":"l2-h3-1","val":6,"next":{"__type__":"ListNode","__id__":"l2-h3-2","val":4,"next":{"__type__":"ListNode","__id__":"l2-h3-3","val":9,"next":null}}}}},"expected":{"__type__":"ListNode","val":7,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":8,"next":{"__type__":"ListNode","val":9,"next":null}}}},"description":"A longer second list contributes its final digit."},{"id":"h4","input":{"l1":{"__type__":"ListNode","__id__":"l1-h4-0","val":9,"next":{"__type__":"ListNode","__id__":"l1-h4-1","val":9,"next":{"__type__":"ListNode","__id__":"l1-h4-2","val":9,"next":{"__type__":"ListNode","__id__":"l1-h4-3","val":9,"next":{"__type__":"ListNode","__id__":"l1-h4-4","val":9,"next":null}}}}},"l2":{"__type__":"ListNode","__id__":"l2-h4-0","val":1,"next":null}},"expected":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":1,"next":null}}}}}},"description":"A carry chain extends through every digit."},{"id":"h5","input":{"l1":{"__type__":"ListNode","__id__":"l1-h5-0","val":1,"next":{"__type__":"ListNode","__id__":"l1-h5-1","val":8,"next":null}},"l2":{"__type__":"ListNode","__id__":"l2-h5-0","val":0,"next":null}},"expected":{"__type__":"ListNode","val":1,"next":{"__type__":"ListNode","val":8,"next":null}},"description":"Adding zero leaves the original reverse-order list unchanged."},{"id":"h6","input":{"l1":{"__type__":"ListNode","__id__":"l1-h6-0","val":5,"next":{"__type__":"ListNode","__id__":"l1-h6-1","val":5,"next":{"__type__":"ListNode","__id__":"l1-h6-2","val":5,"next":null}}},"l2":{"__type__":"ListNode","__id__":"l2-h6-0","val":5,"next":{"__type__":"ListNode","__id__":"l2-h6-1","val":5,"next":{"__type__":"ListNode","__id__":"l2-h6-2","val":5,"next":null}}}},"expected":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":1,"next":{"__type__":"ListNode","val":1,"next":{"__type__":"ListNode","val":1,"next":null}}}},"description":"Equal-length lists can still generate a final carry."},{"id":"h7","input":{"l1":{"__type__":"ListNode","__id__":"l1-h7-0","val":0,"next":null},"l2":{"__type__":"ListNode","__id__":"l2-h7-0","val":7,"next":{"__type__":"ListNode","__id__":"l2-h7-1","val":3,"next":{"__type__":"ListNode","__id__":"l2-h7-2","val":2,"next":null}}}},"expected":{"__type__":"ListNode","val":7,"next":{"__type__":"ListNode","val":3,"next":{"__type__":"ListNode","val":2,"next":null}}},"description":"Adding a zero list preserves a longer reverse-order operand."},{"id":"h8","input":{"l1":{"__type__":"ListNode","__id__":"l1-h8-0","val":9,"next":{"__type__":"ListNode","__id__":"l1-h8-1","val":9,"next":{"__type__":"ListNode","__id__":"l1-h8-2","val":1,"next":null}}},"l2":{"__type__":"ListNode","__id__":"l2-h8-0","val":1,"next":{"__type__":"ListNode","__id__":"l2-h8-1","val":0,"next":{"__type__":"ListNode","__id__":"l2-h8-2","val":9,"next":null}}}},"expected":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":0,"next":{"__type__":"ListNode","val":1,"next":{"__type__":"ListNode","val":1,"next":null}}}},"description":"A middle zero digit still participates in a carry that creates a new most-significant node."}],"hints":[{"level":1,"content":"Add from the head because the digits are already reversed.","revealPattern":false},{"level":2,"content":"Carry must survive after either input list runs out.","revealPattern":true},{"level":3,"content":"A dummy head makes appending each result digit uniform, including the first digit.","revealPattern":true}],"guidedFlow":{"progressRevision":"add-two-numbers-2026-07-19-local-column","approachSelection":{"contract":"approach-selection-v1","question":"Which approach should we implement?","decisionPrompt":"Choose the approach whose state and complexity match the problem constraints.","approaches":[{"id":"convert-to-integers","name":"Convert lists into integers","summary":"Read every digit into a native integer, add the integers, then rebuild the list.","decisionReason":"Turning each list into an integer and back overflows once the numbers get long, which the problem allows.","complexity":{"time":"O(max(m,n))","space":"O(max(m,n))"},"tradeoffs":["Runs in O(max(m,n)) time and uses O(max(m,n)) space."],"isOptimal":false},{"id":"digit-carry-traversal","name":"Digit-by-digit carry traversal","summary":"Traverse both lists once, append each output digit to a result list, and carry overflow to the next position.","decisionReason":"Add the lists digit by digit from the ones place, carrying any overflow forward, and keep going while either list has digits or a carry remains.","complexity":{"time":"O(max(m,n))","space":"O(max(m,n))"},"tradeoffs":["Runs in O(max(m,n)) time and uses O(max(m,n)) space."],"isOptimal":true}]},"codingCheckpoints":[{"id":"dummy-result","title":"Create a dummy result head","description":"Initialize a dummy node and a tail cursor for appending output digits.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"initializes_output_dummy_anchor","scope":"dummy-result","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"seeds_output_tail_from_dummy","scope":"dummy-result","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Special-casing the first output digit","Losing the head of the result list while advancing the tail"],"hint":"The dummy node is not part of the answer; its next pointer is.","semanticSpec":{"intent":"Create a stable output anchor and a separate tail cursor.","doneWhen":"A dummy output node is allocated and a tail cursor is seeded from it.","shouldFailWhen":"The code has no stable anchor or advances the only reference to the result head.","roles":["dummy anchor","output tail"],"mustProve":["output dummy anchor","tail seeded from anchor"],"mustNotAccept":["single moving output cursor without an anchor"],"proofSnippets":[{"id":"py-dummy-result","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy"}]}},{"id":"carry-state","title":"Track carry between digits","description":"Initialize carry to zero and include it in every digit addition.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"initializes_carry_state_zero","scope":"carry-state","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"adds_cursor_digits_with_carry","scope":"carry-state","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Computing each digit independently without carry","Resetting carry before it contributes to the next digit"],"hint":"Carry is the tens digit of the previous total.","semanticSpec":{"intent":"Use a carry state as an input to each digit sum.","doneWhen":"Carry starts at zero and the digit total combines both cursor digits with carry.","shouldFailWhen":"The total omits carry or carry is not initialized.","roles":["carry","current digit total"],"mustProve":["zero carry seed","carry included in digit sum"],"mustNotAccept":["adding cursor digits without carry"],"proofSnippets":[{"id":"py-carry-state","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    x = l1.val if l1 else 0\n    y = l2.val if l2 else 0\n    total = x + y + carry"}]}},{"id":"loop-condition","title":"Loop while input or carry remains","description":"Continue while either list has a node or carry is nonzero.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"loops_while_digit_input_or_carry_remains","scope":"loop-condition","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"reads_missing_cursor_digits_as_zero","scope":"loop-condition","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"advances_input_cursors_after_digit","scope":"loop-condition","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Stopping when the shorter list ends","Stopping before appending a final carry node"],"hint":"A leftover carry is another output digit.","semanticSpec":{"intent":"Continue digit processing while either input cursor or carry can produce another digit.","doneWhen":"The loop guard includes both input cursors and carry, reads missing cursor digits as zero, and advances input cursors.","shouldFailWhen":"The loop stops at the shorter list or cannot safely read a missing cursor digit.","roles":["first input cursor","second input cursor","carry"],"mustProve":["loop guard covers both cursors and carry","missing cursor digit defaults to zero","input cursors advance"],"mustNotAccept":["looping only while both input cursors exist"],"proofSnippets":[{"id":"py-loop-condition","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    while l1 or l2 or carry:\n        x = l1.val if l1 else 0\n        y = l2.val if l2 else 0\n        total = x + y + carry\n        l1 = l1.next if l1 else None\n        l2 = l2.next if l2 else None"}]}},{"id":"append-digit","title":"Append the ones digit","description":"Append total modulo ten and update carry with integer division by ten.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"updates_carry_by_base_division","scope":"append-digit","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"appends_modulo_digit_node","scope":"append-digit","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"advances_output_tail_after_append","scope":"append-digit","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Appending the whole total instead of one digit","Using floating-point division for carry"],"hint":"Each output node stores exactly one decimal digit.","semanticSpec":{"intent":"Emit the current ones digit and carry the tens digit forward.","doneWhen":"The output tail receives a node built from total modulo ten, carry is updated by base-ten division, and the tail advances.","shouldFailWhen":"The code appends the full total, omits the carry update, or leaves the output tail in place.","roles":["digit total","carry","output tail"],"mustProve":["carry from base division","modulo digit append","tail advance"],"mustNotAccept":["append total without modulo","carry updated by modulo"],"proofSnippets":[{"id":"py-append-digit","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    while l1 or l2 or carry:\n        x = l1.val if l1 else 0\n        y = l2.val if l2 else 0\n        total = x + y + carry\n        carry = total // 10\n        current.next = ListNode(total % 10)\n        current = current.next\n        l1 = l1.next if l1 else None\n        l2 = l2.next if l2 else None"}]}},{"id":"return-result","title":"Return the result head","description":"Return dummy.next, the first real result node.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"returns_output_head_from_dummy_next","scope":"return-result","readMode":"linked-list-value-query-v1"}]},"commonMistakes":["Returning the dummy node","Returning the tail cursor after it has advanced to the end"],"hint":"The answer starts after the dummy anchor.","semanticSpec":{"intent":"Return the first real output node after the dummy anchor.","doneWhen":"The function returns the next link of the stable output anchor.","shouldFailWhen":"The code returns the dummy anchor or the advanced tail cursor.","roles":["dummy anchor","result head"],"mustProve":["return anchor.next"],"mustNotAccept":["return dummy","return output tail"],"proofSnippets":[{"id":"py-return-result","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    while l1 or l2 or carry:\n        x = l1.val if l1 else 0\n        y = l2.val if l2 else 0\n        total = x + y + carry\n        carry = total // 10\n        current.next = ListNode(total % 10)\n        current = current.next\n        l1 = l1.next if l1 else None\n        l2 = l2.next if l2 else None\n    return dummy.next"}]}}],"version":2,"slides":[{"id":"read-the-signal","kind":"info","chapter":"The numbers","eyebrow":"Read the problem","title":"Add two numbers stored backward","workspacePanel":"problem","body":["Each number is a linked list of its digits in reverse, least-significant digit first. Add the two numbers and return the sum as a linked list in the same reversed order.","The list 2 → 4 → 3 is the number 342, and 5 → 6 → 4 is 465. Their sum 807 comes back as 7 → 0 → 8."]},{"id":"convert-baseline","kind":"code-trace","chapter":"The numbers","eyebrow":"The obvious way","title":"Read each list into a number, add, rebuild","summary":"Reading both lists into one integer and adding them gets the right answer on small inputs, but watch what that single middle number has to hold once the digits pile up.","workspacePanel":"trace","codeTrace":{"contract":"code-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"num1 = 0\nmult = 1\nwhile l1:\n    num1 = num1 + l1.val * mult\n    mult = mult * 10\n    l1 = l1.next\nnum2 = 0\nmult = 1\nwhile l2:\n    num2 = num2 + l2.val * mult\n    mult = mult * 10\n    l2 = l2.next\ntotal = num1 + num2\ndummy = ListNode(0)\ncurrent = dummy\nwhile total > 0:\n    current.next = ListNode(total % 10)\n    current = current.next\n    total = total // 10\nreturn dummy.next","steps":[{"id":"read-l1","title":"Walk l1, scaling each digit into num1","codeLine":4,"summary":"2 + 4·10 + 3·100 rebuilds 342 into a single native integer.","state":{"lists":[{"name":"l1","values":[2,4,3],"next":[1,2,null],"pointers":{"l1":null}},{"name":"num1","values":[342],"current":0}]}},{"id":"read-l2","title":"Do the same for l2","codeLine":10,"summary":"5 + 6·10 + 4·100 gives num2 = 465.","state":{"lists":[{"name":"l2","values":[5,6,4],"next":[1,2,null],"pointers":{"l2":null}},{"name":"num2","values":[465],"current":0}]}},{"id":"add","title":"Add the two native integers","codeLine":13,"summary":"342 + 465 = 807, computed in one step.","state":{"lists":[{"name":"total","values":[807],"current":0}]}},{"id":"rebuild","title":"Peel digits off total back into a list","codeLine":17,"summary":"807 → 7, 0, 8: each total % 10 becomes a node, then total // 10 drops it.","state":{"lists":[{"name":"total","values":[807],"current":0},{"name":"result","values":[0,7,0,8],"next":[1,2,3,null],"pointers":{"current":3},"current":3}]}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"let num1 = 0;\nlet mult = 1;\nwhile (l1 != null) {\n    num1 = num1 + (l1.val * mult);\n    mult = mult * 10;\n    l1 = l1.next;\n}\nlet num2 = 0;\nmult = 1;\nwhile (l2 != null) {\n    num2 = num2 + (l2.val * mult);\n    mult = mult * 10;\n    l2 = l2.next;\n}\nlet total = num1 + num2;\nconst dummy = new ListNode(0, null);\nlet current = dummy;\nwhile (total > 0) {\n    current.next = new ListNode(floorModulo(total, 10), null);\n    current = current.next;\n    total = Math.floor(total / 10);\n}\nreturn dummy.next;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":12,"12":13,"13":15,"14":16,"15":17,"16":18,"17":19,"18":20,"19":21,"20":23},"statementBundles":{"6":{"startLine":6,"endLine":7},"12":{"startLine":13,"endLine":14},"19":{"startLine":21,"endLine":22}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"let num1 = 0;\nlet mult = 1;\nwhile (l1 != null) {\n    num1 = num1 + (l1.val * mult);\n    mult = mult * 10;\n    l1 = l1.next;\n}\nlet num2 = 0;\nmult = 1;\nwhile (l2 != null) {\n    num2 = num2 + (l2.val * mult);\n    mult = mult * 10;\n    l2 = l2.next;\n}\nlet total = num1 + num2;\nconst dummy = new ListNode(0, null);\nlet current = dummy;\nwhile (total > 0) {\n    current.next = new ListNode(floorModulo(total, 10), null);\n    current = current.next;\n    total = Math.floor(total / 10);\n}\nreturn dummy.next;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":12,"12":13,"13":15,"14":16,"15":17,"16":18,"17":19,"18":20,"19":21,"20":23},"statementBundles":{"6":{"startLine":6,"endLine":7},"12":{"startLine":13,"endLine":14},"19":{"startLine":21,"endLine":22}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"int num1 = 0;\nint mult = 1;\nwhile (l1 != null) {\n    num1 = num1 + (l1.val * mult);\n    mult = mult * 10;\n    l1 = l1.next;\n}\nint num2 = 0;\nmult = 1;\nwhile (l2 != null) {\n    num2 = num2 + (l2.val * mult);\n    mult = mult * 10;\n    l2 = l2.next;\n}\nint total = num1 + num2;\nListNode dummy = new ListNode(0);\nListNode current = dummy;\nwhile (total > 0) {\n    current.next = new ListNode((total % 10), null);\n    current = current.next;\n    total = Math.floorDiv(total, 10);\n}\nreturn dummy.next;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":12,"12":13,"13":15,"14":16,"15":17,"16":18,"17":19,"18":20,"19":21,"20":23},"statementBundles":{"6":{"startLine":6,"endLine":7},"12":{"startLine":13,"endLine":14},"19":{"startLine":21,"endLine":22}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"int num1 = 0;\nint mult = 1;\nwhile (l1 != null) {\n    num1 = num1 + l1.val * mult;\n    mult = mult * 10;\n    l1 = l1.next;\n}\nint num2 = 0;\nmult = 1;\nwhile (l2 != null) {\n    num2 = num2 + l2.val * mult;\n    mult = mult * 10;\n    l2 = l2.next;\n}\nint total = num1 + num2;\nListNode dummy = new(0, null);\nListNode current = dummy;\nwhile (total > 0) {\n    current.next = new ListNode(FloorModulo(total, 10), null);\n    current = current.next;\n    total = total / 10;\n}\nreturn dummy.next;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":12,"12":13,"13":15,"14":16,"15":17,"16":18,"17":19,"18":20,"19":21,"20":23},"statementBundles":{"6":{"startLine":6,"endLine":7},"12":{"startLine":13,"endLine":14},"19":{"startLine":21,"endLine":22}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"int num1 = 0;\nint mult = 1;\nwhile (l1) {\n    num1 = num1 + (l1->val * mult);\n    mult = mult * 10;\n    l1 = l1->next;\n}\nint num2 = 0;\nmult = 1;\nwhile (l2) {\n    num2 = num2 + (l2->val * mult);\n    mult = mult * 10;\n    l2 = l2->next;\n}\nint total = num1 + num2;\nListNode* dummy = new ListNode(0, nullptr);\nListNode* current = dummy;\nwhile (total > 0) {\n    current->next = new ListNode(floorModulo(total, 10), nullptr);\n    current = current->next;\n    total = total / 10;\n}\nreturn dummy->next;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":8,"8":9,"9":10,"10":11,"11":12,"12":13,"13":15,"14":16,"15":17,"16":18,"17":19,"18":20,"19":21,"20":23},"statementBundles":{"6":{"startLine":6,"endLine":7},"12":{"startLine":13,"endLine":14},"19":{"startLine":21,"endLine":22}}}}}},{"id":"convert-cost-probe","kind":"case-probe","chapter":"The numbers","eyebrow":"Name the catch","title":"The lists can hold up to 100 digits. What breaks when total has to hold them all?","workspacePanel":"trace","summary":"A 100-node list is a 100-digit number.","options":[{"id":"overflow","label":"total has to store the whole number at once, which overflows a fixed-width integer","rationale":"A 100-digit value dwarfs a 64-bit integer, so in most languages total wraps or loses precision long before the lists end. The digit-at-a-time work was never the bottleneck.","isCorrect":true},{"id":"slower","label":"Nothing breaks; it is just slower than walking the lists once","rationale":"It is the same linear walk, so speed is not the issue. The break is that one integer cannot hold a 100-digit value in a fixed-width type."},{"id":"order","label":"The digits come out in the wrong order after rebuilding","rationale":"Peeling with % 10 and // 10 already yields reverse order, matching the required output. The real failure is the integer overflowing before we ever rebuild."}],"answer":"Collapsing the list into one integer overflows a fixed-width type once the number is long. We need a way to produce the result without ever holding either whole number.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"overflow-state","state":{"lists":[{"name":"total","values":["99…99 (100 digits)"],"highlighted":[0],"current":0}]}}]}},{"id":"local-column-probe","kind":"case-probe","chapter":"The idea","eyebrow":"Use what is local","title":"At the first nodes 2 and 5, what is enough to produce the first output digit?","workspacePanel":"visualizer","options":[{"id":"current-digits-carry","label":"Use 2 + 5 + 0, emit its column digit, then advance","rationale":"They total 7, so the first output node is 7 and the outgoing carry is 0. Advancing both list positions then exposes exactly the information needed for the next digit.","isCorrect":true},{"id":"whole-numbers","label":"Every digit from both lists, rebuilt into 342 and 465","rationale":"That recreates the overflowing whole-number representation. The first output digit depends only on the current column and its incoming carry.","isCorrect":false},{"id":"list-lengths","label":"Only the lengths of the two lists","rationale":"Lengths reveal how many positions remain, but they do not determine the sum at the current position. The digit values do.","isCorrect":false}],"answer":"The two current digits plus the incoming carry are enough to emit one result digit. Then advance through the lists and repeat, carrying only the overflow into the next position.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"first-column-state","state":{"lists":[{"name":"l1","values":[2,4,3],"next":[1,2,null],"pointers":{"l1":0},"current":0},{"name":"l2","values":[5,6,4],"next":[1,2,null],"pointers":{"l2":0},"current":0},{"name":"result","values":["?"],"pointers":{"current":0},"current":0}],"carry":0}}]},"summary":"The incoming carry is 0, and the result must also store its least-significant digit first."},{"id":"why-reverse","kind":"case-probe","chapter":"The idea","eyebrow":"Read the order","title":"The digits are stored least-significant first. Why does that help addition?","workspacePanel":"visualizer","summary":"You add two numbers the way you would on paper.","options":[{"id":"ones-first","label":"You add from the ones place forward, the direction the carry flows","rationale":"On paper you start at the ones column and carry left; reversed lists put the ones first, so walking forward matches that carry direction exactly.","isCorrect":true},{"id":"sorted","label":"The digits are sorted, so it is faster","rationale":"Digit order is positional, not sorted by value; reversal matters because the ones place comes first."},{"id":"no-reason","label":"It makes no difference to the addition","rationale":"It matters: starting at the ones place lets the carry move forward through the list as you go."}],"answer":"You add from the ones place forward, which is the direction a carry moves, so a single forward walk handles every column.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"reverse-state","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":0},"current":0,"activeEdges":[[0,1,"read"]]},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":0},"current":0},{"name":"result","values":["•"],"pointers":{"current":0},"current":0}]}}]}},{"id":"carry-split","kind":"case-probe","chapter":"The idea","eyebrow":"Find the rule","title":"Two digits plus a carry can exceed 9. What becomes the result digit, and what carries?","workspacePanel":"visualizer","summary":"Suppose one column computes 9 + 1 + 0 = 10.","options":[{"id":"mod-div","label":"The digit is total % 10; the carry is total // 10","rationale":"The ones place of the total is what stays in this column, and the tens place is carried to the next, so 10 leaves a 0 and carries 1.","isCorrect":true},{"id":"swap","label":"The digit is total // 10; the carry is total % 10","rationale":"That is backward: the remainder is the digit that stays, and the quotient is what carries left."},{"id":"keep","label":"Keep the full total, 10, in the node","rationale":"A node holds a single digit, so a total of 10 must split into a 0 here and a 1 carried forward."}],"answer":"Digit = total % 10, carry = total // 10. A total of 10 leaves a 0 in this place and carries a 1 to the next.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"carry-state","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":1},"current":1},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":null}},{"name":"result","values":["•",0],"pointers":{"current":1},"current":1}]}}]}},{"id":"loop-with-carry","kind":"case-probe","chapter":"The idea","eyebrow":"Find the finish","title":"One list is longer, and a carry can outlast both. How does the loop keep going correctly?","workspacePanel":"visualizer","summary":"Adding 95 + 7, the lists differ in length and the sum has an extra digit.","options":[{"id":"or-carry","label":"Treat a missing digit as 0, and loop while either list has a digit or the carry is nonzero","rationale":"A shorter list contributes 0 in its empty places, and including the carry in the loop condition lets a leftover carry create one last node.","isCorrect":true},{"id":"both","label":"Stop as soon as either list ends","rationale":"Stopping when one ends drops the rest of the longer list and any final carry, losing leading digits."},{"id":"longest","label":"Loop exactly the length of the longer list","rationale":"That still misses a carry that survives past both lists, like the leading 1 in 102."}],"answer":"Missing digit counts as 0, and the loop runs while either list has a digit or the carry is nonzero, so a final carry adds a last node.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"loop-state","state":{"lists":[{"name":"l1","values":[5,9],"next":[1,null],"pointers":{"l1":null}},{"name":"l2","values":[7],"next":[null],"pointers":{"l2":null}},{"name":"result","values":["•",2,0],"pointers":{"current":2},"current":2}]}}]}},{"id":"approach-selection","kind":"choice","source":"approach-selection","chapter":"The idea","eyebrow":"Pick the approach","title":"Which shape should we build?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"approach-structure","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":0},"current":0,"activeEdges":[[0,1,"read"]]},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":0},"current":0},{"name":"result","values":["•"],"pointers":{"current":0},"current":0}]}}]}},{"id":"dry-run-setup","kind":"info","chapter":"Trace","eyebrow":"Watch it move","title":"Column by column, carry forward","workspacePanel":"visualizer","body":["A dummy anchors the result and carry starts at 0.","Step through 99 (9 → 9) plus 1 (1) one position at a time. Use the rules you just chose to decide each output node and whether the loop continues."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"setup-state","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":0},"current":0,"activeEdges":[[0,1,"read"]]},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":0},"current":0},{"name":"result","values":["•"],"pointers":{"current":0},"current":0}]}}]}},{"id":"trace","kind":"dry-run","source":"trace-example","chapter":"Trace","eyebrow":"Add the columns","title":"Add 99 and 1","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"ones","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":1},"current":1},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":null}},{"name":"result","values":["•",0],"pointers":{"current":1},"current":1}]}},{"id":"tens","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":null}},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":null}},{"name":"result","values":["•",0,0],"pointers":{"current":2},"current":2}]}},{"id":"final-carry","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":null}},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":null}},{"name":"result","values":["•",0,0,1],"pointers":{"current":3},"current":3}]}}]},"beats":[{"id":"ones","title":"Ones place: 9 + 1. What digit is appended, and what carries?","summary":"The first column.","prompt":"carry starts at 0.","options":[{"id":"zero-one","label":"Append 0; carry becomes 1","rationale":"9 + 1 + 0 = 10, so the ones place 0 is appended and the tens place 1 is carried forward.","isCorrect":true},{"id":"ten","label":"Append 10","rationale":"A node holds one digit, so 10 splits into a 0 appended here and a 1 carried."},{"id":"one-zero","label":"Append 1; carry becomes 0","rationale":"The appended digit is the ones place, 0. The 1 moves into the next column as carry."}],"answer":"Append 0, carry 1. 9 + 1 + 0 = 10, so 0 stays and 1 moves to the next column."},{"id":"tens","title":"Tens place: 9, but l2 has run out. What is the column total?","summary":"l2 has no node at this position.","prompt":"l1 has 9, l2 is empty, and carry is 1.","options":[{"id":"ten","label":"9 + 0 + 1 = 10, so append 0 and carry 1 again","rationale":"The empty l2 contributes 0, and the carry adds 1, giving 10, so another 0 is appended and the carry stays 1.","isCorrect":true},{"id":"nine","label":"9, since l2 is gone","rationale":"l2 missing means 0, not skip; the carry of 1 still adds in, making the total 10."},{"id":"stop","label":"Stop, l2 ended","rationale":"l1 still has a digit and there is a carry, so the loop continues with l2 treated as 0."}],"answer":"10. The empty l2 is 0, plus 9 plus the carry 1, so append 0 and carry 1 once more."},{"id":"final-carry","title":"Both lists are empty now, but carry is 1. Does the loop stop?","summary":"No input nodes remain.","prompt":"l1 and l2 are both null; carry is 1.","options":[{"id":"no","label":"No: the leftover carry appends a final 1","rationale":"The loop runs while a carry remains, so 0 + 0 + 1 = 1 is appended, giving 0 → 0 → 1, the number 100.","isCorrect":true},{"id":"yes","label":"Yes: both lists are empty","rationale":"A nonzero carry keeps the loop going; dropping it would lose the leading 1 and report 0 instead of 100."},{"id":"discard","label":"Stop and discard the carry","rationale":"Discarding the carry loses the most significant digit; it must become its own node."}],"answer":"No. A leftover carry keeps the loop alive, appending a final 1, so the result is 0 → 0 → 1 = 100."}]},{"id":"drop-carry-pitfall","kind":"pitfall","chapter":"Trace","eyebrow":"Spot the trap","workspacePanel":"trace","title":"Could you just return the node you started from?","summary":"The dummy head made appending uniform, and every result node hangs off it in order. But the dummy itself is a placeholder that was never a real digit. Watch what the caller sees when the anchor ships with the answer.","pitfall":"The loop builds the digits correctly, but the final line returns dummy instead of dummy.next.","whyItMatters":"Adding 342 + 465, the loop builds 7 → 0 → 8 correctly. Returning dummy hands back the placeholder node too, so the answer reads 0 → 7 → 0 → 8, with an extra leading 0 the number never had. Return dummy.next to skip the anchor.","negativeTrace":{"contract":"negative-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"dummy = ListNode(0)\ncurrent = dummy\ncarry = 0\nwhile l1 or l2 or carry:\n    x = l1.val if l1 else 0\n    y = l2.val if l2 else 0\n    total = x + y + carry\n    carry = total // 10\n    current.next = ListNode(total % 10)\n    current = current.next\n    l1 = l1.next if l1 else None\n    l2 = l2.next if l2 else None\nreturn dummy","steps":[{"id":"built","title":"The loop builds the digits correctly: 7 → 0 → 8","codeLine":9,"summary":"Adding 342 + 465, the digits land right behind the dummy anchor, in order.","state":{"lists":[{"name":"l1","values":[2,4,3],"next":[1,2,null],"pointers":{"l1":null}},{"name":"l2","values":[5,6,4],"next":[1,2,null],"pointers":{"l2":null}},{"name":"result","values":[0,7,0,8],"next":[1,2,3,null],"pointers":{"current":3},"current":3}]}},{"id":"returns-anchor","title":"But the last line returns dummy, the placeholder node","codeLine":13,"summary":"dummy still holds its seed value 0 and sits in front of the real first digit.","state":{"lists":[{"name":"result","values":[0,7,0,8],"next":[1,2,3,null],"pointers":{"current":0},"current":0}]}},{"id":"extra-zero","title":"The caller sees 0 → 7 → 0 → 8, an extra leading 0","codeLine":13,"summary":"807 comes back as 0 → 7 → 0 → 8 because the anchor shipped with the answer.","state":{"lists":[{"name":"result","values":[0,7,0,8],"next":[1,2,3,null],"highlighted":[0],"pointers":{"current":0},"current":0}]}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"const dummy = new ListNode(0, null);\nlet current = dummy;\nlet carry = 0;\nwhile ((l1 != null || l2 != null) || carry) {\n    const x = l1 != null ? l1.val : 0;\n    const y = l2 != null ? l2.val : 0;\n    const total = (x + y) + carry;\n    carry = Math.floor(total / 10);\n    current.next = new ListNode(floorModulo(total, 10), null);\n    current = current.next;\n    l1 = l1 != null ? l1.next : null;\n    l2 = l2 != null ? l2.next : null;\n}\nreturn dummy;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,"11":11,"12":12,"13":14},"statementBundles":{"12":{"startLine":12,"endLine":13}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"const dummy = new ListNode(0, null);\nlet current = dummy;\nlet carry = 0;\nwhile ((l1 != null || l2 != null) || carry) {\n    const x = l1 != null ? l1.val : 0;\n    const y = l2 != null ? l2.val : 0;\n    const total = (x + y) + carry;\n    carry = Math.floor(total / 10);\n    current.next = new ListNode(floorModulo(total, 10), null);\n    current = current.next;\n    l1 = l1 != null ? l1.next : null;\n    l2 = l2 != null ? l2.next : null;\n}\nreturn dummy;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,"11":11,"12":12,"13":14},"statementBundles":{"12":{"startLine":12,"endLine":13}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"ListNode dummy = new ListNode(0);\nListNode current = dummy;\nint carry = 0;\nwhile (((l1 != null || l2 != null) || carry != 0)) {\n    int x = l1 != null ? l1.val : 0;\n    int y = l2 != null ? l2.val : 0;\n    int total = (x + y) + carry;\n    carry = Math.floorDiv(total, 10);\n    current.next = new ListNode((total % 10), null);\n    current = current.next;\n    l1 = l1 != null ? l1.next : null;\n    l2 = l2 != null ? l2.next : null;\n}\nreturn dummy;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,"11":11,"12":12,"13":14},"statementBundles":{"12":{"startLine":12,"endLine":13}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"ListNode dummy = new(0, null);\nListNode current = dummy;\nint carry = 0;\nwhile (l1 != null || l2 != null || carry != 0) {\n    int x = l1 != null ? l1.val : 0;\n    int y = l2 != null ? l2.val : 0;\n    int total = x + y + carry;\n    carry = total / 10;\n    current.next = new ListNode(FloorModulo(total, 10), null);\n    current = current.next;\n    l1 = l1 != null ? l1.next : null;\n    l2 = l2 != null ? l2.next : null;\n}\nreturn dummy;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,"11":11,"12":12,"13":14},"statementBundles":{"12":{"startLine":12,"endLine":13}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"ListNode* dummy = new ListNode(0, nullptr);\nListNode* current = dummy;\nint carry = 0;\nwhile (((l1 || l2) || carry)) {\n    int x = (l1 ? l1->val : 0);\n    int y = (l2 ? l2->val : 0);\n    int total = (x + y) + carry;\n    carry = total / 10;\n    current->next = new ListNode(floorModulo(total, 10), nullptr);\n    current = current->next;\n    l1 = (l1 ? l1->next : nullptr);\n    l2 = (l2 ? l2->next : nullptr);\n}\nreturn dummy;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10,"11":11,"12":12,"13":14},"statementBundles":{"12":{"startLine":12,"endLine":13}}}}}},{"id":"edge-case-intro","kind":"info","chapter":"Edge cases","eyebrow":"Try to break it","title":"Which inputs could break this?","workspacePanel":"visualizer","body":["Adding 99 and 1 exercised the carry. Two more inputs are worth a thought before coding.","Numbers of different lengths, where one list runs out first, and a single column that overflows, where the carry outlives both lists."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"intro-state","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":0},"current":0,"activeEdges":[[0,1,"read"]]},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":0},"current":0},{"name":"result","values":["•"],"pointers":{"current":0},"current":0}]}}]}},{"id":"unequal-lengths-edge","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"The numbers have different digit counts. What happens in the extra columns?","workspacePanel":"visualizer","summary":"l1 is 1 → 2 → 3 (321) and l2 is just 7.","options":[{"id":"zero-fill","label":"The shorter list contributes 0, so the longer digits carry through","rationale":"Once l2 ends, its columns count as 0, so l1’s remaining digits (plus any carry) are added straight through.","isCorrect":true},{"id":"stop","label":"Addition stops when l2 ends","rationale":"Stopping would drop l1’s higher digits; the empty l2 just contributes 0 while l1 continues."},{"id":"pad","label":"l2 must be padded with nodes first","rationale":"No padding is needed; a missing digit is simply read as 0 in the loop."}],"answer":"The shorter list contributes 0 in its empty columns, so the longer list’s digits carry through to the end.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"unequal-state","state":{"lists":[{"name":"l1","values":[1,2,3],"next":[1,2,null],"pointers":{"l1":1},"current":1,"activeEdges":[[1,2,"read"]]},{"name":"l2","values":[7],"next":[null],"pointers":{"l2":null}},{"name":"result","values":["•",8],"pointers":{"current":1},"current":1}]}}]}},{"id":"all-carry-edge","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"How long is the result of 5 + 5?","workspacePanel":"visualizer","summary":"Both lists are a single node: 5 and 5.","options":[{"id":"two","label":"Two digits: 0 → 1, the number 10","rationale":"5 + 5 = 10, so the ones place is 0 and the carry 1 outlasts both lists, adding a second node.","isCorrect":true},{"id":"one","label":"One digit: 0","rationale":"The carry of 1 survives past both single-node lists and must be appended, making the result two digits."},{"id":"ten","label":"One digit: 10","rationale":"A node holds a single digit, so 10 becomes 0 → 1 across two nodes."}],"answer":"Two digits, 0 → 1 (the number 10). The single column overflows, and the carry becomes its own final node.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"all-carry-state","state":{"lists":[{"name":"l1","values":[5],"next":[null],"pointers":{"l1":null}},{"name":"l2","values":[5],"next":[null],"pointers":{"l2":null}},{"name":"result","values":["•",0,1],"pointers":{"current":2},"current":2}]}}]}},{"id":"ready-to-code","kind":"info","chapter":"Code","eyebrow":"Get ready to code","title":"Turn the rule into code","workspacePanel":"visualizer","summary":"Column by column, carry included.","body":["You have the method: add the two lists a column at a time from the ones place, splitting each total into the digit that stays and the carry that moves on.","A missing digit counts as zero, and the walk keeps going while either list has a digit or a carry is still waiting."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"ready-state","state":{"lists":[{"name":"l1","values":[9,9],"next":[1,null],"pointers":{"l1":0},"current":0,"activeEdges":[[0,1,"read"]]},{"name":"l2","values":[1],"next":[null],"pointers":{"l2":0},"current":0},{"name":"result","values":["•"],"pointers":{"current":0},"current":0}]}}]}},{"id":"coding","kind":"coding","source":"coding-checkpoints","chapter":"Code","title":"Build the digit-carry add","eyebrow":"Code the rule","summary":"Now turn the column add into code: anchor the result with a dummy and its tail, start the carry at zero and fold it into each column total, loop while either list or the carry still has a digit (missing nodes read as 0), split each total into the digit you append and the carry you keep, then return the node after the dummy.","workspacePanel":"code","checkpoints":[{"id":"dummy-result","title":"Create a dummy result head","description":"Initialize a dummy node and a tail cursor for appending output digits.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"initializes_output_dummy_anchor","scope":"dummy-result","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"seeds_output_tail_from_dummy","scope":"dummy-result","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Special-casing the first output digit","Losing the head of the result list while advancing the tail"],"hint":"The dummy node is not part of the answer; its next pointer is.","semanticSpec":{"intent":"Create a stable output anchor and a separate tail cursor.","doneWhen":"A dummy output node is allocated and a tail cursor is seeded from it.","shouldFailWhen":"The code has no stable anchor or advances the only reference to the result head.","roles":["dummy anchor","output tail"],"mustProve":["output dummy anchor","tail seeded from anchor"],"mustNotAccept":["single moving output cursor without an anchor"],"proofSnippets":[{"id":"py-dummy-result","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy"}]}},{"id":"carry-state","title":"Track carry between digits","description":"Initialize carry to zero and include it in every digit addition.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"initializes_carry_state_zero","scope":"carry-state","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"adds_cursor_digits_with_carry","scope":"carry-state","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Computing each digit independently without carry","Resetting carry before it contributes to the next digit"],"hint":"Carry is the tens digit of the previous total.","semanticSpec":{"intent":"Use a carry state as an input to each digit sum.","doneWhen":"Carry starts at zero and the digit total combines both cursor digits with carry.","shouldFailWhen":"The total omits carry or carry is not initialized.","roles":["carry","current digit total"],"mustProve":["zero carry seed","carry included in digit sum"],"mustNotAccept":["adding cursor digits without carry"],"proofSnippets":[{"id":"py-carry-state","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    x = l1.val if l1 else 0\n    y = l2.val if l2 else 0\n    total = x + y + carry"}]}},{"id":"loop-condition","title":"Loop while input or carry remains","description":"Continue while either list has a node or carry is nonzero.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"loops_while_digit_input_or_carry_remains","scope":"loop-condition","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"reads_missing_cursor_digits_as_zero","scope":"loop-condition","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"advances_input_cursors_after_digit","scope":"loop-condition","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Stopping when the shorter list ends","Stopping before appending a final carry node"],"hint":"A leftover carry is another output digit.","semanticSpec":{"intent":"Continue digit processing while either input cursor or carry can produce another digit.","doneWhen":"The loop guard includes both input cursors and carry, reads missing cursor digits as zero, and advances input cursors.","shouldFailWhen":"The loop stops at the shorter list or cannot safely read a missing cursor digit.","roles":["first input cursor","second input cursor","carry"],"mustProve":["loop guard covers both cursors and carry","missing cursor digit defaults to zero","input cursors advance"],"mustNotAccept":["looping only while both input cursors exist"],"proofSnippets":[{"id":"py-loop-condition","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    while l1 or l2 or carry:\n        x = l1.val if l1 else 0\n        y = l2.val if l2 else 0\n        total = x + y + carry\n        l1 = l1.next if l1 else None\n        l2 = l2.next if l2 else None"}]}},{"id":"append-digit","title":"Append the ones digit","description":"Append total modulo ten and update carry with integer division by ten.","validation":{"type":"semantic","validator":"all","params":[{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"updates_carry_by_base_division","scope":"append-digit","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"appends_modulo_digit_node","scope":"append-digit","readMode":"linked-list-value-query-v1"}]},{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"advances_output_tail_after_append","scope":"append-digit","readMode":"linked-list-value-query-v1"}]}]},"commonMistakes":["Appending the whole total instead of one digit","Using floating-point division for carry"],"hint":"Each output node stores exactly one decimal digit.","semanticSpec":{"intent":"Emit the current ones digit and carry the tens digit forward.","doneWhen":"The output tail receives a node built from total modulo ten, carry is updated by base-ten division, and the tail advances.","shouldFailWhen":"The code appends the full total, omits the carry update, or leaves the output tail in place.","roles":["digit total","carry","output tail"],"mustProve":["carry from base division","modulo digit append","tail advance"],"mustNotAccept":["append total without modulo","carry updated by modulo"],"proofSnippets":[{"id":"py-append-digit","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    while l1 or l2 or carry:\n        x = l1.val if l1 else 0\n        y = l2.val if l2 else 0\n        total = x + y + carry\n        carry = total // 10\n        current.next = ListNode(total % 10)\n        current = current.next\n        l1 = l1.next if l1 else None\n        l2 = l2.next if l2 else None"}]}},{"id":"return-result","title":"Return the result head","description":"Return dummy.next, the first real result node.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"linked-list","fact":"returns_output_head_from_dummy_next","scope":"return-result","readMode":"linked-list-value-query-v1"}]},"commonMistakes":["Returning the dummy node","Returning the tail cursor after it has advanced to the end"],"hint":"The answer starts after the dummy anchor.","semanticSpec":{"intent":"Return the first real output node after the dummy anchor.","doneWhen":"The function returns the next link of the stable output anchor.","shouldFailWhen":"The code returns the dummy anchor or the advanced tail cursor.","roles":["dummy anchor","result head"],"mustProve":["return anchor.next"],"mustNotAccept":["return dummy","return output tail"],"proofSnippets":[{"id":"py-return-result","language":"python","code":"def addTwoNumbers(l1, l2):\n    dummy = ListNode(0)\n    current = dummy\n    carry = 0\n    while l1 or l2 or carry:\n        x = l1.val if l1 else 0\n        y = l2.val if l2 else 0\n        total = x + y + carry\n        carry = total // 10\n        current.next = ListNode(total % 10)\n        current = current.next\n        l1 = l1.next if l1 else None\n        l2 = l2.next if l2 else None\n    return dummy.next"}]}}]},{"id":"complete","kind":"complete","chapter":"Code","title":"What to carry forward","eyebrow":"Take it with you","summary":"Add the lists column by column from the ones place, splitting each total into a digit and a carry, and loop while a carry survives.","workspacePanel":"code","body":["Reversed digits let a single forward walk do paper addition: ones first, carry moving forward.","The carry is loop state. The loop must keep running for a carry that outlasts both lists."],"takeaways":[{"title":"A total splits into digit and carry","body":"digit = total % 10 stays in the node; carry = total // 10 moves to the next column."},{"title":"The carry can outlive both lists","body":"Loop while l1 or l2 or carry, treating a missing digit as 0, so a final carry like the 1 in 100 becomes its own node."}],"nextAction":"Submit when you are ready. TraceCode will run the final checks against the code you wrote."}],"slidesOrigin":"authored"},"executionStyle":"solution-method"}
