[leetCode刷题笔记]2017.01.27(Add)

本文介绍了解决两个链表交点问题的高效算法,并提供了一个链表相加问题的解决方案,该方案实现了时间复杂度为O(n),空间复杂度为O(1)的目标。

160. Intersection of Two Linked Lists

这道题挺有意思的,其实原理很简单,就是将两个链表的长度求出来,然后求出差值(A - B = N)。再做一个指针指向较长那个链表(A)的第N个node。接下来用这个指针再和短的链表(B)一个一个比较,最后找到目标。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int numA = leng(headA);
        int numB = leng(headB);
        ListNode currA = headA;
        ListNode currB = headB;
        if (numA > numB) {
            currA = move(headA, numA - numB);
        }
        else {
            currB = move(headB, numB - numA);
        }
        while (currA != null) {
            if (currA == currB) {
                return currA;
            }
            else {
                currA = currA.next;
                currB = currB.next;
            }
        }
        return null;
    }
    private int leng(ListNode head) {
        int output = 0;
        ListNode curr = head;
        while (curr != null) {
            curr = curr.next;
            output++;
        }
        return output;
    }
    private ListNode move(ListNode head, int num) {
        int i = 0;
        ListNode curr = head;
        while (i < num) {
            curr = curr.next;
            i++;
        }
        return curr;
    }
}


445. Add Two Numbers II


这个让我做了好久,最后实现了时间O(n)空间O(1);题目上面说不要对输入链表进行翻转,其实要对输出链表进行翻转。。。

先对两个链表进行计数,然后大的那个先往新的里面加多出的几位,然后再加共有的几位数。注意,输出的链表第一位要为空,一是为了后面好加node二是为了后面进位需要!

接下来再对输出数组进行反转,反转后再梳理,进行进位运算。接下来再将输出数组转会来,如果第一位不是零,输出完整链表。如果第一位不是,从第二位开始输出。

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
       int length1 = count(l1);
       int length2 = count(l2);
       ListNode output = new ListNode(0);
       ListNode curr = output;
       ListNode curr1 = l1;
       ListNode curr2 = l2;
       if (length1 > length2) {
           for (int i = 0; i < length1 - length2; i++) {
               curr.next = new ListNode(curr1.val);
               curr = curr.next;
               curr1 = curr1.next;
           }
           
       }
       else {
           for (int i = 0; i < length2 - length1; i++) {
               curr.next = new ListNode(curr2.val);
               curr = curr.next;
               curr2 = curr2.next;
           }
           
       }
       while (curr1 != null) {
           int add = curr1.val + curr2.val;
           curr.next = new ListNode(add);
           curr = curr.next;
           curr1 = curr1.next;
           curr2 = curr2.next;
           
       }
       ListNode outputL = reverse(output);
       ListNode currO = outputL;
       while (currO != null) {
           if (currO.val > 9) {
               currO.val = currO.val - 10;
               currO.next.val++;
           }
           currO = currO.next;
       }
       output = reverse(outputL);
       if (output.val != 0) {
           return output;
       }
       else {
           return output.next;
       }
    }
    private int count(ListNode head) {
        int output = 0;
        ListNode curr = head;
        while (curr != null) {
            curr = curr.next;
            output++;
        }
        return output;
    }
    private ListNode reverse(ListNode head) {
        ListNode prev = null;
        
        while (head != null) {
            ListNode temp = head.next;
            head.next = prev;
            prev = head;
            head = temp;
        }
        
        return prev;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值