两数相加

118 阅读1分钟

JAVA

class ListNode {
    int val;
    ListNode next;
    ListNode(int val) { this.val = val; }
}


public class Test {

    public static void main(String[] args) throws Exception {
        ListNode l1 = buildListNode(new int[]{2, 4, 3});
        ListNode l2 = buildListNode(new int[]{5, 6, 4});
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }

            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }

        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        System.out.println(head);
    }

    /**
     * 构造ListNode
     */
    private static ListNode buildListNode(int[] input) {
        ListNode first = null, last = null, newNode;
        if (input.length > 0) {
            for (int i = 0; i < input.length; i++) {
                newNode = new ListNode(input[i]);
                newNode.next = null;
                if (first == null) {
                    first = newNode;
                    last = newNode;
                } else {
                    last.next = newNode;
                    last = newNode;
                }
            }
        }
        return first;
    }
}

Golang

package main

import "fmt"

type ListNode struct {
   Val  int
   Next *ListNode
}

func addTowNumbers(l1, l2 *ListNode) (head *ListNode) {
   var tail *ListNode
   carry := 0
   for l1 != nil || l2 != nil {
      n1, n2 := 0, 0
      if l1 != nil {
         n1 = l1.Val
         l1 = l1.Next
      }
      if l2 != nil {
         n2 = l2.Val
         l2 = l2.Next
      }
      sum := n1 + n2 + carry
      sum, carry = sum%10, sum/10
      if head == nil {
         head = &ListNode{Val: sum}
         tail = head
      } else {
         tail.Next = &ListNode{Val: sum}
         tail = tail.Next
      }
   }
   if carry > 0 {
      tail.Next = &ListNode{Val: carry}
   }
   return head
}

func main() {
   fmt.Println("Start")
   var l1 *ListNode = nil
   var l2 *ListNode = nil
   var pre *ListNode = nil
   a := []int{2, 4, 3}
   b := []int{5, 6, 4}
   for _, num := range a {
      p := new(ListNode)
      if l1 == nil {
         l1 = p
      } else {
         pre.Next = p
      }
      p.Val = num
      pre = p
      p = p.Next
   }
   for _, num := range b {
      p := new(ListNode)
      if l2 == nil {
         l2 = p
      } else {
         pre.Next = p
      }
      p.Val = num
      pre = p
      p = p.Next
   }

   l3 := addTowNumbers(l1, l2)
   for {
      if l3 == nil {
         break
      }
      fmt.Println(l3.Val)
      l3 = l3.Next
   }
}

TypeScript

function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
    let head = null, tail = null;
    let carry = 0;
    while (l1 || l2) {
        const n1 = l1 ? l1.val : 0;
        const n2 = l2 ? l2.val : 0;
        const sum = n1 + n2 + carry;
        if (!head) {
            head = tail = new ListNode(sum % 10);
        } else {
            tail.next = new ListNode(sum % 10);
            tail = tail.next;
        }
        carry = Math.floor(sum / 10);
        if (l1) {
            l1 = l1.next;
        }
        if (l2) {
            l2 = l2.next;
        }
    }
    if (carry > 0) {
        tail.next = new ListNode(carry);
    }
    return head;
};