【牛客题霸题解】链表相加

两个链表生成相加链表

http://www.nowcoder.com/practice/c56f6c70fb3f4849bc56e33ff2a50b6b

先把链表进行反转,然后再相加
反转链表的步骤参考NC78
https://blog.nowcoder.net/n/31e6abf3dd5d4970a03f7a483ca8e0ab
相加的时候每一位上的值等于两个链表那位上的值相加再加上之前的进位%10

c++

class Solution {
public:
    //反转链表
    ListNode* ReverseList(ListNode* pHead) {
        ListNode* Pre = NULL;
        ListNode* Now = pHead;
        ListNode* Next = NULL;
        while(Now!=NULL)
        {
            Next = Now->next;
            Now->next = Pre;
            Pre = Now;
            Now = Next;
        }
        pHead = Pre;
        return pHead;
    }
    ListNode* addInList(ListNode* head1, ListNode* head2) {
        head1 = ReverseList(head1);
        head2 = ReverseList(head2);
        ListNode* res = new ListNode(0);
        ListNode* now = res;
        int d = 0;//存储进位
        while(head1!=NULL||head2!=NULL)
        {
            int num = d;
            if(head1!= NULL){
                num += head1->val;
                head1 = head1->next;
            }
            if(head2!= NULL){
                num += head2->val;
                head2 = head2->next;
            }
            d = num/10;
            num = num%10;
            ListNode *t = new ListNode(num);
            now->next = t;
            now = now->next;
            cout<<now->val<<endl;
        }
        if(d!=0)
        {
            ListNode t(d);
            t.next = NULL;
            now ->next = &t;
        }
        else{
            now ->next = NULL;
        }
        res = ReverseList(res->next);
        return res;
    }
};

java

import java.util.*;
public class Solution {

    public ListNode ReverseList(ListNode head) {
        ListNode Pre = null;
        ListNode Now = head;
        ListNode Next = null;
        while(Now != null){
            Next = Now.next;
            Now.next = Pre;
            Pre = Now;
            Now = Next; 
        }
        head = Pre;
        return head;
    }
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        head1 = ReverseList(head1);
        head2 = ReverseList(head2);
        ListNode res = new ListNode(0);
        ListNode now = res;
        int d = 0;//存储进位

        while(head1!=null||head2!=null)
        {
            int num = d;
            if(head1!= null){
                num += head1.val;
                head1 = head1.next;
            }
            if(head2!= null){
                num += head2.val;
                head2 = head2.next;
            }
            d = num/10;
            num = num%10;
            ListNode t = new ListNode(num);
            now.next = t;
            now = now.next;
        }
        if(d!=0)
        {
            ListNode t =new ListNode(d);
            t.next = null;
            now.next = t;
        }
        else{
            now.next = null;
        }
        res = ReverseList(res.next);
        return res;
    }
}

python

class Solution:
    def ReverseList(self, pHead):
        Pre = None
        Now = pHead
        Next = None
        while Now != None:
            Next = Now.next
            Now.next = Pre
            Pre = Now
            Now = Next
        pHead = Pre
        return pHead
    def addInList(self , head1 , head2 ):
        head1 = self.ReverseList(head1)
        head2 = self.ReverseList(head2)
        res = ListNode(0)
        now = res
        d = 0
        while head1!=None or head2!=None:
            num = d
            if head1!= None:
                num = num + head1.val
                head1 = head1.next
            if head2!= None:
                num = num+head2.val
                head2 = head2.next
            d = num//10
            num = num%10
            t = ListNode(num)
            now.next = t
            now = now.next
        if d!=0:
            t = ListNode(d)
            t.next = None
            now.next = t
        else:
            now.next = None
        res = self.ReverseList(res.next)
        return res
牛客题霸题解 文章被收录于专栏

QAQ

全部评论

相关推荐

想干测开的tomca...:让我来压力你!!!: 这份简历看着“技术词堆得满”,实则是“虚胖没干货”,槽点一抓一大把: 1. **项目描述是“技术名词报菜名”,没半分自己的实际价值** 不管是IntelliDoc还是人人探店,全是堆Redis、Elasticsearch、RAG这些时髦词,但你到底干了啥?“基于Redis Bitmap管理分片”是你写了核心逻辑还是只调用了API?“QPS提升至1500”是你独立压测优化的,还是团队成果你蹭着写?全程没“我负责XX模块”“解决了XX具体问题”,纯把技术文档里的术语扒下来凑字数,看着像“知道名词但没实际动手”的实习生抄的。 2. **短项目塞满超纲技术点,可信度直接***** IntelliDoc就干了5个月,又是RAG又是大模型流式响应又是RBAC权限,这堆活儿正经团队分工干都得小半年,你一个后端开发5个月能吃透这么多?明显是把能想到的技术全往里面塞,生怕别人知道你实际只做了个文件上传——这种“技术堆砌式造假”,面试官一眼就能看出水分。 3. **技能栏是“模糊词混子集合”,没半点硬核度** “熟悉HashMap底层”“了解JVM内存模型”——“熟悉”是能手写扩容逻辑?“了解”是能排查GC问题?全是模棱两可的词,既没对应项目里的实践,也没体现深度,等于白写;项目里用了Elasticsearch的KNN检索,技能栏里提都没提具体掌握程度,明显是“用过但不懂”的硬凑。 4. **教育背景和自我评价全是“无效信息垃圾”** GPA前10%这么好的牌,只列“Java程序设计”这种基础课,分布式、微服务这些后端核心课提都不提,白瞎了专业优势;自我评价那堆“积极认真、细心负责”,是从招聘网站抄的模板吧?没有任何和项目挂钩的具体事例,比如“解决过XX bug”“优化过XX性能”,纯废话,看完等于没看。 总结:这简历是“技术名词缝合怪+自我感动式凑数”,看着像“背了后端技术栈名词的应届生”,实则没干货、没重点、没可信度——面试官扫30秒就会丢一边,因为连“你能干嘛”都没说清楚。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务