LinkedList:链表相加

链表相加 :7->1->6代表数字617 5—>9->2代表295 617+295 = 912 得出2->1->9 返回链表

public static Node test3(Node node1,Node node2) {
        Node lastNode = null;
        Node headNode = null;
        int last = 0;
        while (node1!=null || node2!=null) {
            if (headNode == null) {
                headNode = new Node((node1.value+node2.value+last)%10);
                lastNode = headNode;
                last = (node1.value+node2.value+last)/10;
            } else {
                Node node = new Node((node1.value+node2.value+last)%10);
                lastNode.next = node;
                lastNode = node;
                last = (node1.value+node2.value+last)/10;
            }
            node1 = node1.next;
            node2 = node2.next;
            
        }
        if (last == 1) {
            lastNode.next = new Node(1);
        }
        return headNode;
    }

你可能感兴趣的:(LinkedList:链表相加)