博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Merge Two Sorted Lists
阅读量:6986 次
发布时间:2019-06-27

本文共 1281 字,大约阅读时间需要 4 分钟。

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

 

 to see which companies asked this question

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* mergeTwoLists(struct ListNode *l1, struct ListNode *l2){        if (l1 == NULL && l2 == NULL) {            return NULL;        } else if (l1 == NULL) {            return l2;        } else if (l2 == NULL) {            return l1;        }        ListNode* phead;        ListNode* node;        ListNode* p1 = l1;        ListNode* p2 = l2;        if (p1->val <= p2->val) {            phead = p1;            p1 = p1->next;        } else {            phead = p2;            p2 = p2->next;        }        node = phead;        while (p1 != NULL && p2 != NULL) {            if (p1->val <= p2->val) {                node->next = p1;                p1 = p1->next;            } else {                node->next = p2;                p2 = p2->next;            }            node = node->next;        }        node->next = p1 ? p1 : p2;        return phead;    }};

 

转载于:https://www.cnblogs.com/SpeakSoftlyLove/p/5119724.html

你可能感兴趣的文章
好程序员web前端技术之CSS3过渡
查看>>
java B2B2C源码电子商务平台 - Zuul回退机制
查看>>
记录Docker in Docker 安装(CentOS7)
查看>>
简单的写一个发布订阅器
查看>>
重学前端-js的类型问题
查看>>
Function类型
查看>>
Python学习
查看>>
ES6之let和const
查看>>
不用软件,手动修复双系统引导进win7,xp的多种方法
查看>>
python 访问需要HTTP Basic Authentication认证的资源
查看>>
java中比较字符串的大小用String的compareTo()
查看>>
plist使用
查看>>
Linux RAR 安装和使用
查看>>
【OC】【一秒就会】【collectionView 头部吸住功能】
查看>>
51CTO下载 好资料分享
查看>>
linux 下转换UTC到本地时间
查看>>
Linux的起源与各发行版的基本知识
查看>>
单播包、广播包、组播包、洪泛包
查看>>
iptables命令结构之命令
查看>>
RabbitMQ之Exchange分类
查看>>