2020-08-26

Leecode445

分别使用栈和链表反转两种方法

/*
输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 8 -> 0 -> 7
 */
public class Solution {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(7);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(4);
        ListNode l4 = new ListNode(3);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = null;

        ListNode n1 = new ListNode(5);
        ListNode n2 = new ListNode(6);
        ListNode n3 = new ListNode(4);
        n1.next = n2;
        n2.next = n3;
        n3.next = null;

        ListNode ans = addTwoNumbers2(l1, n1);
        print(ans);
    }
    public static void print(ListNode head){
        ListNode p = head;
        while(p != null){
            System.out.print(p.val + "->");
            p = p.next;
        }
        System.out.println("null");
    }

    //使用栈+头插法
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<ListNode> s1 = new Stack<>();
        Stack<ListNode> s2 = new Stack<>();

        ListNode p = l1;
        ListNode q = l2;
        ListNode head = new ListNode(-1);
        head.next = null;

        while(p != null){
            s1.push(p);
            p = p.next;
        }
        while(q != null){
            s2.push(q);
            q = q.next;
        }

        int c = 0;
        while(!s1.isEmpty() || !s2.isEmpty() || c > 0){
            int sum = (s1.isEmpty() ? 0 : s1.pop().val) + (s2.isEmpty() ? 0 : s2.pop().val) + c;
            c = sum / 10;
            int re = sum % 10;
            ListNode l = new ListNode(re);
            l.next = head.next;
            head.next = l;
        }
        return head.next;
    }

    //链表反转+头插法
    public static ListNode addTwoNumbers2(ListNode l1, ListNode l2){
        ListNode list1 = new ListNode(-1);
        list1.next = null;
        ListNode list2 = new ListNode(-1);
        list2.next = null;

        ListNode p = l1;
        ListNode q = l2;

        //使用新的链表存储反转的链表
        while(p != null){
            ListNode temp = new ListNode(p.val);
            temp.next = list1.next;
            list1.next = temp;
            p = p.next;
        }
        while(q != null){
            ListNode temp = new ListNode(q.val);
            temp.next = list2.next;
            list2.next = temp;
            q = q.next;
        }

        //和链表使用头插法插入新链表
        ListNode relist1 = list1.next;
        ListNode relist2 = list2.next;
        ListNode head = new ListNode(-1);//和链表存储
        head.next = null;
        p = relist1;
        q = relist2;
        int c = 0;
        while(p != null || q != null || c > 0){
            int sum = (p == null ? 0 : p.val) + (q == null ? 0 : q.val) + c;
            int re = sum % 10;
            c = sum / 10;
            ListNode t = new ListNode(re);
            t.next = head.next;
            head.next = t;
            if(p != null)
                p = p.next;
            if(q != null)
                q = q.next;
        }
        return head.next;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值