package org.example.test;

import com.alibaba.fastjson.JSONObject;

public class LinkedNodeTest {

    public static void main(String[] args) {
        ListNode node = new ListNode(9);
        ListNode node1 = new ListNode(3);
        node.next = node1;
        ListNode node11 = new ListNode(7);
        node1.next = node11;

        ListNode node2 = new ListNode(6);
        ListNode node3 = new ListNode(3);
        node2.next = node3;
        ListNode node4 = addInList(node, node2);
        System.out.println(JSONObject.toJSONString(node4));

        ListNode node12 = new ListNode(0);
        ListNode point = node12;
        point.next = new ListNode(1);
        System.out.println(node12.next.val);
    }

    public static class ListNode {
        int val;
        ListNode next = null;

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

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public ListNode getNext() {
            return next;
        }

        public void setNext(ListNode next) {
            this.next = next;
        }
    }

    /**
     * 先把2个链表反转,然后遍历相加,然后再反转。
     *
     * @param head1
     * @param head2
     * @return
     */
    public static ListNode addInList(ListNode head1, ListNode head2) {
        head1 = getReverseListNode(head1);
        head2 = getReverseListNode(head2);
        ListNode cur1 = head1;
        ListNode cur2 = head2;
        ListNode node = new ListNode(0);
        ListNode point = node;
        int tmp = 0;
        while (cur1 != null && cur2 != null) {
            int x = cur1.val;
            int y = cur2.val;
            int sum = x + y + tmp;
            if (sum >= 10) {
                sum = sum % 10;
                tmp = 1;
            } else {
                tmp = 0;

            }
            point.next = new ListNode(sum);
            point = point.next;
            cur1 = cur1.next;
            cur2 = cur2.next;

        }
        while (cur1 != null) {
            int a = cur1.val;
            int sum = a + tmp;
            if (sum >= 10) {
                sum = sum % 10;
                tmp = 1;
            } else {
                tmp = 0;

            }
            point.next = new ListNode(sum);
            point = point.next;
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            int a = cur2.val;
            int sum = a + tmp;
            if (sum >= 10) {
                sum = sum % 10;
                tmp = 1;
            } else {
                tmp = 0;

            }
            point.next = new ListNode(sum);
            point = point.next;
            cur2 = cur2.next;
        }
        if (tmp == 1) {
            point.next = new ListNode(1);
        }
        node = getReverseListNode(node.next);
        return node;
    }

    private static ListNode getReverseListNode(ListNode head1) {
        ListNode pre = null;
        ListNode next;
        while (head1 != null) {
            next = head1.next;
            head1.next = pre;
            pre = head1;
            head1 = next;
        }
        return pre;
    }
}