java算法之链表两数相加

/***

  • 给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。 你可以假设除了数字 0
  • 之外,这两个数字都不会以零开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 +
  • 465 = 807
  • @author 96414

*/

class ListNode {
    public int val;
    public ListNode next;
}

public class ListNodeTest {
    ListNode head; // 链表的表头指针

    public ListNode init() {
        head = new ListNode();
        head.next = null;
        return head;
    }// 初始化

    public void add(int x) { // 将x加入升序链表
        ListNode pre, p, q;
//      for (pre = head, p = head.next; p != null; p = p.next, pre = pre.next)
//          if (p.val > x)
//              break;
        pre = head;
        p = head.next;
        q = new ListNode();
        q.val = x;
        q.next = p;
        pre.next = q; // 将q插入到pre和p之间
    }

    public ListNode find(int x) {// 在表中重找x,找到则返回其前驱结点的指针,找不到则返回null
        ListNode pre, p;
        pre = head;
        p = head.next;
        while (p != null && p.val != x) {
            pre = pre.next;
            p = p.next;
        }
        if (p == null)
            return null;
        return pre;
    }

    public void del(int x) {// 从链表中删除值为x的元素
        ListNode pre = find(x);
        if (pre == null)
            return; // 没找到
        else
            pre.next = pre.next.next; // 实施删除
    }

    public void showInfo() {
        for (ListNode p = head.next; p != null; p = p.next)
            System.out.print(p.val + " ");
    }

    public static void main(String[] args) {
        ListNodeTest test = new ListNodeTest();
        ListNodeTest test2 = new ListNodeTest(); 
        ListNode l1 = test.init();
        ListNode l2 = test2.init();
        System.out.print("请输入一组数,以-1结束:");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        while (x != -1) {
            test.add(x);
            x = sc.nextInt();
        }
        int y = sc.nextInt();
        while (y != -1) {
            test2.add(y);
            y = sc.nextInt();
        }
        System.out.print("有序链表l1为:");
        test.showInfo();
        System.out.println();
        System.out.print("有序链表l2为:");
        test2.showInfo();
        System.out.println();
        System.out.println("结果为:");
        addTwoNumbers(l1.next, l2.next);
    }
    public static void addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode();
        dummyHead.next=null;
        dummyHead.val=0;
        ListNode p = l1, q = l2, curr = dummyHead;
        int carry = 0;
        while (p != null || q != null) {
            int x = (p != null) ? p.val : 0;
            int y = (q != null) ? q.val : 0;
            System.out.println(p.val);
            System.out.println(q.val);
            int sum = carry + x + y;
            carry = sum / 10;
            curr.next = new ListNode();
            curr.next.val = sum % 10;
            curr = curr.next;
            if (p != null) p = p.next;
            if (q != null) q = q.next;
        }
        if (carry > 0) {
            curr.next = new ListNode();
            curr.next.val = carry;
        }
        for (ListNode x1 = dummyHead.next; x1 != null; x1 = x1.next) {
            System.out.print(x1.val + " ");
        }
    }
}

参考:https://leetcode-cn.com/problems/add-two-numbers/
https://liuyanzhao.com/2230.html

你可能感兴趣的:(java算法之链表两数相加)