题解 | 链表相加(一)

import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 *   public ListNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

/**
 * NC317 链表相加(一)
 * @author d3y1
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param l1 ListNode类
     * @param l2 ListNode类
     * @return ListNode类
     */
    public ListNode ListAdd (ListNode l1, ListNode l2) {
        return solution1(l1,l2);
        // return solution2(l1,l2);
    }

    /**
     * 迭代
     * @param l1
     * @param l2
     * @return
     */
    private ListNode solution1(ListNode l1, ListNode l2){
        ListNode head = new ListNode(-1);
        ListNode tail = head;
        ListNode node;
        int val1,val2,sum,carry=0;
        while(l1!=null || l2!=null || carry>0){
            if(carry == 0){
                if(l1 == null){
                    tail.next = l2;
                    break;
                }
                if(l2 == null){
                    tail.next = l1;
                    break;
                }
            }

            val1 = 0;
            if(l1 != null){
                val1 = l1.val;
                l1 = l1.next;
            }

            val2 = 0;
            if(l2 != null){
                val2 = l2.val;
                l2 = l2.next;
            }

            sum = val1+val2+carry;
            carry = sum/10;
            node = new ListNode(sum%10);
            // 尾插法
            tail.next = node;
            tail = node;
        }

        return head.next;
    }

    //////////////////////////////////////////////////////////////////////////////

    private int val1,val2,sum,carry=0;
    private ListNode head = new ListNode(-1);
    private ListNode tail = head;
    private ListNode node;

    /**
     * 递归: 超出内存限制!
     * @param l1
     * @param l2
     * @return
     */
    private ListNode solution2(ListNode l1, ListNode l2){
        add(l1,l2);
        return head.next;
    }

    private void add(ListNode l1, ListNode l2){
        if(l1==null && l2==null && carry==0){
            return;
        }

        val1 = 0;
        if(l1 != null){
            val1 = l1.val;
            l1 = l1.next;
        }

        val2 = 0;
        if(l2 != null){
            val2 = l2.val;
            l2 = l2.next;
        }

        sum = val1+val2+carry;
        carry = sum/10;
        node = new ListNode(sum%10);
        // 尾插法
        tail.next = node;
        tail = node;

        add(l1,l2);
    }
}

全部评论

相关推荐

2024-12-30 14:37
哈尔滨理工大学 C++
#include #include using namespace std;struct Student {    int id;                 string name;            int score;              Student* next;     };int main() {    Student* head = nullptr;      Student* tail = nullptr;     while (true) {        int id;        string name;        int score; cin >> id;        if (id == 0) {            break;        } cin >> name >> score;          Student* newStudent = new Student{id, name, score, nullptr};        if (head == nullptr) {            head = newStudent;            tail = newStudent;        } else { tail->next = newStudent;            tail = newStudent;        }    }    Student* current = head;    while (current != nullptr) {        cout id name score current = current->next;    }    current = head;    while (current != nullptr) {        Student* temp = current; current = current->next;        delete temp;    }    return 0;}
点赞 评论 收藏
分享
评论
2
收藏
分享
牛客网
牛客企业服务