2816. 翻倍以链表形式表示的数字.txt

UP 返回
https://leetcode.cn/problems/double-a-number-represented-as-a-linked-list/

public class LeetCode2816 {

    public static void main(String[] args) {
        ListNode node = new ListNode(9, null);
        ListNode node1 = new ListNode(8, node);
        ListNode node2 = new ListNode(1, node1);
        System.out.println(doubleIt(node2));
    }

    private static ListNode doubleIt(ListNode head) {
        List<ListNode> list = new ArrayList<>();
        while (head != null) {
            list.add(head);
            head = head.next;
        }
        int go = 0;
        for (int i = list.size() - 1; i >= 0; i--) {
            int doubleNum = list.get(i).val * 2 + go;
            go = doubleNum / 10;
            list.get(i).val = doubleNum % 10;
        }
        if (go == 1) {
            ListNode listNode = new ListNode();
            listNode.val = 1;
            listNode.next = list.get(0);
            return listNode;
        }
        return list.get(0);
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

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

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
DOWN 返回