[左神面试指南] 链表[上]篇

CD48 打印两个有序链表的公共部分

/* 归并 */
public class CD48_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }


    public static ArrayList<Integer> solution(ListNode l1, ListNode l2)
    {
        ArrayList<Integer> ans = new ArrayList<>();
        while (l1 != null && l2 != null)
        {
            if (l1.val < l2.val)
                l1 = l1.next;
            else if (l1.val > l2.val)
                l2 = l2.next;
            else
            {
                ans.add(l1.val);
                l1 = l1.next;
                l2 = l2.next;
            }
        }
        return ans;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int N, M;
        N = in.nextInt();
        int[] l1 = new int[N];
        for (int i = 0; i < N; i++)
            l1[i] = in.nextInt();
        M = in.nextInt();
        int[] l2 = new int[M];
        for (int i = 0; i < M; i++)
            l2[i] = in.nextInt();
        ArrayList<Integer> res = solution(ListNode.createSingleList(l1)[0], ListNode.createSingleList(l2)[0]);
        System.out.println(res.stream().map(String::valueOf).collect(Collectors.joining(" ")));
    }
}

CD49 在单链表和双链表中删除倒数第 K 个节点

/* 双指针 */
public class CD49_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static ListNode solution(ListNode head, int K)
    {
        ListNode pre = head, last = head;
        while (K-- > 0)
            last = last.next;
        if (last == null) return head.next;
        while (last.next != null)
        {
            pre = pre.next;
            last = last.next;
        }
        pre.next = pre.next.next;
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N = in.nextInt(), K = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l)[0], K);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

/* 单指针 */
public class CD49_2
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }


    public static ListNode solution(ListNode head, int K)
    {
        ListNode pre = head;
        while (pre != null)
        {
            K--;
            pre = pre.next;
        }
        pre = head;
        if (K == 0) head = head.next;
        else if (K < 0)
        {
            while (++K < 0)
                pre = pre.next;
            pre.next = pre.next.next;
        }
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N = in.nextInt(), K = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l)[0], K);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

CD106 删除链表的中间节点和 a/b 处的节点

/* 模拟 */
public class CD106_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }


    public static ListNode solution(ListNode head, int K)
    {
        ListNode pre = head;
        if (K == 1) return head.next;
        K--;
        while (--K > 0)
            pre = pre.next;
        pre.next = pre.next.next;
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N = in.nextInt(), K = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l)[0], K);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

/* 快慢双指针(删除中间节点) */
public class CD106_2
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static ListNode solution(ListNode head)
    {
        if (head == null || head.next == null)
            return head;
        if (head.next.next == null)
            return head.next;
        ListNode pre = head, cur = head.next.next;
        while (cur.next != null && cur.next.next != null)
        {
            pre = pre.next;
            cur = cur.next.next;
        }
        pre.next = pre.next.next;
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l)[0]);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

CD107 反转单向和双向链表

/* 模拟 */
public class CD107_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static class DoubleListNode
    {
        public int val;
        public DoubleListNode next = null;
        public DoubleListNode prev = null;

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

        public static DoubleListNode[] createDoubleList(int[] nums)
        {
            DoubleListNode head = new DoubleListNode(-1), tail = head;
            for (int num : nums)
            {
                DoubleListNode temp = new DoubleListNode(num);
                tail.next = temp;
                temp.prev = tail;
                tail = temp;
            }
            head.next.prev = null;
            return new DoubleListNode[]{head.next, tail};
        }
    }


    public static ListNode solutionSingle(ListNode head)
    {
        ListNode temp, tail = head.next;
        head.next = null;
        while (tail != null)
        {
            temp = tail.next;
            tail.next = head;
            head = tail;
            tail = temp;
        }
        return head;
    }

    public static DoubleListNode solutionDouble(DoubleListNode head)
    {
        DoubleListNode temp, tail = head.next;
        head.next = null;
        while (tail != null)
        {
            temp = tail.next;
            tail.next = head;
            head.prev = tail;
            head = tail;
            tail = temp;
        }
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N, M;
        N = in.nextInt();
        int[] l1 = new int[N];
        for (int i = 0; i < N; i++)
            l1[i] = in.nextInt();
        ListNode res1 = solutionSingle(ListNode.createSingleList(l1)[0]);
        while (res1 != null)
        {
            out.print(res1.val + (res1.next == null ? "" : " "));
            res1 = res1.next;
        }
        out.println();

        M = in.nextInt();
        int[] l2 = new int[M];
        for (int i = 0; i < M; i++)
            l2[i] = in.nextInt();
        DoubleListNode res2 = solutionDouble(DoubleListNode.createDoubleList(l2)[0]);
        while (res2 != null)
        {
            out.print(res2.val + (res2.next == null ? "" : " "));
            res2 = res2.next;
        }
        out.flush();
    }
}

CD108 反转部分单向链表

/* 模拟 */
public class CD108_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static ListNode solution(ListNode head, int L, int R)
    {
        int cnt = 0;
        ListNode fakeHead = new ListNode(-1), pre = fakeHead, revStart = null, revEnd = null, tail = null, temp;
        fakeHead.next = head;
        while (head != null)
        {
            cnt++;
            if (cnt == L)
            {
                tail = pre;
                revStart = head;
            }
            if (cnt == R)
            {
                revEnd = head.next;
                head.next = null;
                break;
            }
            pre = pre.next;
            head = head.next;
        }
        temp = revStart;
        ListNode reverse = reverse(revStart);
        temp.next = revEnd;
        tail.next = reverse;
        return fakeHead.next;
    }

    public static ListNode reverse(ListNode head)
    {
        ListNode temp, tail = head.next;
        head.next = null;
        while (tail != null)
        {
            temp = tail.next;
            tail.next = head;
            head = tail;
            tail = temp;
        }
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N, L, R;
        N = in.nextInt();
        int[] l1 = new int[N];
        for (int i = 0; i < N; i++)
            l1[i] = in.nextInt();
        L = in.nextInt();
        R = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l1)[0], L, R);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

CD109 环形单链表的约瑟夫问题

/* 模拟 */
public class CD109_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            tail.next = head.next;
            return new ListNode[]{head.next, tail};
        }
    }

    public static void solution(ListNode head, int M)
    {
        ListNode pre, temp = head;
        while (temp.next != head)
            temp = temp.next;
        pre = temp;
        int cnt = 0;
        while (pre.next != pre)
        {
            cnt++;
            if (cnt == M)
            {
                cnt = 0;
                pre.next = head.next;
                head = pre.next;
                continue;
            }
            pre = pre.next;
            head = head.next;
        }
        System.out.println(pre.next.val);
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int N, M;
        N = in.nextInt();
        M = in.nextInt();
        int[] l1 = new int[N];
        for (int i = 0; i < N; i++)
            l1[i] = i + 1;
        solution(ListNode.createSingleList(l1)[0], M);
    }
}

CD110 环形单链表的约瑟夫问题(进阶)

/* 约瑟夫算法 */
public class CD110_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            tail.next = head.next;
            return new ListNode[]{head.next, tail};
        }
    }

    public static void solution(ListNode head, int M)
    {
        int len = 1, th;
        ListNode temp = head;
        while (temp.next != head)
        {
            len++;
            temp = temp.next;
        }
        th = LastRemaining_Solution(len, M);
        while (th-- > 0)
            head = head.next;
        System.out.println(head.val);
    }

    public static int LastRemaining_Solution(int n, int m)
    {
        if (n == 1) return 0;
        else return (LastRemaining_Solution(n - 1, m) + m) % n;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int N, M;
        N = in.nextInt();
        M = in.nextInt();
        int[] l1 = new int[N];
        for (int i = 0; i < N; i++)
            l1[i] = i + 1;
        solution(ListNode.createSingleList(l1)[0], M);
    }
}

CD111 判断一个链表是否为回文结构

/* 整条链表放入栈 */
public class CD111_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static boolean solution(ListNode head)
    {
        Stack<Integer> stack = new Stack<>();
        ListNode pre = head;
        while (pre != null)
        {
            stack.add(pre.val);
            pre = pre.next;
        }
        while (head != null)
        {
            if (head.val != stack.pop())
                return false;
            head = head.next;
        }
        return true;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int N;
        N = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        System.out.println(solution(ListNode.createSingleList(l)[0]));
    }
}

/* 利用快慢双指针, 只把一半链表放入栈 */
public class CD111_2
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static boolean solution(ListNode head)
    {
        ListNode last = head, fast = head;
        Stack<Integer> stack = new Stack<>();
        while (fast != null && fast.next != null)
        {
            last = last.next;
            fast = fast.next.next;
        }
        if (fast != null) last = last.next;
        while (last != null)
        {
            stack.add(last.val);
            last = last.next;
        }
        while (!stack.isEmpty())
        {
            if (head.val != stack.pop())
                return false;
            head = head.next;
        }
        return true;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int N;
        N = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        System.out.println(solution(ListNode.createSingleList(l)[0]));
    }
}

CD112 判断一个链表是否为回文结构(进阶)

/* 将右半区链表反转 */
public class CD112_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static boolean solution(ListNode head)
    {
        ListNode last = head, fast = head, node, temp;
        boolean ans = true;
        Stack<Integer> stack = new Stack<>();
        while (fast != null && fast.next != null)
        {
            last = last.next;
            fast = fast.next.next;
        }
        temp = (fast != null ? reverseList(last.next) : reverseList(last));
        node = temp;
        fast = head;
        while (node != null)
        {
            if (fast.val != node.val)
            {
                ans = false;
                break;
            }
            node = node.next;
            fast = fast.next;
        }
        // 将右半区链表还原
        reverseList(temp);
        return ans;
    }

    public static ListNode reverseList(ListNode head)
    {
        ListNode tail = head.next, temp;
        head.next = null;
        while (tail != null)
        {
            temp = tail.next;
            tail.next = head;
            head = tail;
            tail = temp;
        }
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int N;
        N = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        System.out.println(solution(ListNode.createSingleList(l)[0]));
    }
}

CD113 将单向链表按某值划分成左边小、中间相等、右边大的形式

/* 利用数组, 模拟快排(不稳定) */
public class CD113_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static ListNode solution(ListNode head, int pivot)
    {
        ArrayList<ListNode> arr = new ArrayList<>();
        while (head != null)
        {
            arr.add(head);
            head = head.next;
        }
        ListNode[] Nodes = arr.toArray(new ListNode[arr.size()]);
        int idx = 0, small = -1, big = arr.size();
        while (idx != big)
        {
            if (Nodes[idx].val < pivot)
                swapNode(Nodes, idx++, ++small);
            else if (Nodes[idx].val > pivot)
                swapNode(Nodes, idx, --big);
            else idx++;
        }
        for (int i = 1; i < Nodes.length; i++)
            Nodes[i - 1].next = Nodes[i];
        Nodes[Nodes.length - 1].next = null;
        return Nodes[0];
    }

    public static void swapNode(ListNode[] nodes, int i, int j)
    {
        ListNode temp;
        temp = nodes[i];
        nodes[i] = nodes[j];
        nodes[j] = temp;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N, pivot;
        N = in.nextInt();
        pivot = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l)[0], pivot);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

/* 将链表分为大于pivot, 小于pivot, 等于pivot 三段(稳定) */
public class CD113_2
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static ListNode solution(ListNode head, int pivot)
    {
        ListNode small = new ListNode(-1), big = new ListNode(-1), eq = new ListNode(-1), temp;
        ListNode tailSmall = small, tailBig = big, tailEq = eq;
        while (head != null)
        {
            temp = head.next;
            head.next = null;
            if (head.val < pivot)
            {
                tailSmall.next = head;
                tailSmall = head;
            }
            else if (head.val > pivot)
            {
                tailBig.next = head;
                tailBig = head;
            }
            else
            {
                tailEq.next = head;
                tailEq = head;
            }
            head = temp;
        }
        tailEq.next = big.next;
        tailSmall.next = eq.next;
        return small.next;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N, pivot;
        N = in.nextInt();
        pivot = in.nextInt();
        int[] l = new int[N];
        for (int i = 0; i < N; i++)
            l[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l)[0], pivot);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

CDXXX 复制含有随机指针节点的链表

/* 剑指offer链表篇 JZ35 复杂链表的复制 */
/* 分成三步 */
public class JZ35_1
{
    public static RandomListNode Clone(RandomListNode pHead)
    {
        RandomListNode head = pHead, res = null, tail = null;
        while (head != null)
        {
            RandomListNode temp = new RandomListNode(-head.label);
            temp.next = head.next;
            head.next = temp;
            head = head.next.next;
        }

        head = pHead;
        while (head != null)
        {
            if (head.random == null)
                head.next.random = null;
            else
                head.next.random = head.random.next;
            head = head.next.next;
        }
        head = pHead;
        while (head != null)
        {
            if (res == null)
            {
                res = tail = head.next;
            }
            else
            {
                tail.next = head.next;
                tail = tail.next;
            }
            head.next = head.next.next;
            head = head.next;
        }
        return res;
    }
}

/* HashMap */
public class JZ35_2
{
    public static RandomListNode Clone(RandomListNode pHead)
    {
        HashMap<RandomListNode, RandomListNode> map = new HashMap<>();
        RandomListNode p = pHead, res = null, tail = null;
        while (p != null)
        {
            RandomListNode temp = new RandomListNode(p.label);
            if (res == null)
                res = tail = temp;
            else
            {
                tail.next = temp;
                tail = tail.next;
            }
            map.put(p, temp);
            p = p.next;
        }
        p = pHead;
        tail = res;
        while (p != null)
        {
            if (p.random == null)
                tail.random = null;
            else
                tail.random = map.get(p.random);
            p = p.next;
            tail = tail.next;
        }
        return res;
    }
}

CD114 两个单链表生成相加链表

/* 栈 */
public class CD114_1
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static ListNode solution(ListNode head1, ListNode head2)
    {
        ListNode head = new ListNode(-1);
        Stack<Integer> s1 = new Stack<>();
        Stack<Integer> s2 = new Stack<>();
        int add = 0, up = 0;
        while (head1 != null)
        {
            s1.add(head1.val);
            head1 = head1.next;
        }
        while (head2 != null)
        {
            s2.add(head2.val);
            head2 = head2.next;
        }
        while (!s1.isEmpty() || !s2.isEmpty())
        {
            add = up + (s1.isEmpty() ? 0 : s1.pop()) + (s2.isEmpty() ? 0 : s2.pop());
            up = add / 10;
            add %= 10;
            ListNode temp = new ListNode(add);
            temp.next = head.next;
            head.next = temp;
        }
        if (up != 0)
        {
            ListNode temp = new ListNode(up);
            temp.next = head.next;
            head.next = temp;
        }
        return head.next;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N, M;
        N = in.nextInt();
        M = in.nextInt();
        int[] l1 = new int[N];
        for (int i = 0; i < N; i++)
            l1[i] = in.nextInt();
        int[] l2 = new int[M];
        for (int i = 0; i < M; i++)
            l2[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l1)[0], ListNode.createSingleList(l2)[0]);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}

/* 反转链表 */
public class CD114_2
{
    public static class ListNode
    {
        public int val;
        public ListNode next = null;

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

        public static ListNode[] createSingleList(int[] nums)
        {
            ListNode head = new ListNode(-1), tail = head;
            for (int num : nums)
            {
                tail.next = new ListNode(num);
                tail = tail.next;
            }
            return new ListNode[]{head.next, tail};
        }
    }

    public static ListNode solution(ListNode head1, ListNode head2)
    {
        ListNode rev1 = reverseList(head1), rev2 = reverseList(head2), head = new ListNode(-1);
        int up = 0, add = 0;
        while (rev1 != null || rev2 != null)
        {
            add = up + (rev1 == null ? 0 : rev1.val) + (rev2 == null ? 0 : rev2.val);
            up = add / 10;
            add %= 10;
            ListNode temp = new ListNode(add);
            temp.next = head.next;
            head.next = temp;
            rev1 = rev1 == null ? null : rev1.next;
            rev2 = rev2 == null ? null : rev2.next;
        }
        if (up != 0)
        {
            ListNode temp = new ListNode(up);
            temp.next = head.next;
            head.next = temp;
        }
        return head.next;
    }

    public static ListNode reverseList(ListNode head)
    {
        ListNode tail = head.next, temp;
        head.next = null;
        while (tail != null)
        {
            temp = tail.next;
            tail.next = head;
            head = tail;
            tail = temp;
        }
        return head;
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int N, M;
        N = in.nextInt();
        M = in.nextInt();
        int[] l1 = new int[N];
        for (int i = 0; i < N; i++)
            l1[i] = in.nextInt();
        int[] l2 = new int[M];
        for (int i = 0; i < M; i++)
            l2[i] = in.nextInt();
        ListNode res = solution(ListNode.createSingleList(l1)[0], ListNode.createSingleList(l2)[0]);
        while (res != null)
        {
            out.print(res.val + (res.next == null ? "" : " "));
            res = res.next;
        }
        out.flush();
    }
}
posted @ 2023-11-08 12:32  Vivid-BinGo  阅读(10)  评论(0编辑  收藏  举报