当前位置: 首页 > news >正文

网站短期就业培训班wordpress 增加侧边栏

网站短期就业培训班,wordpress 增加侧边栏,南京网站建设方案,江阴市做网站的1.题目 https://leetcode.cn/problems/merge-two-sorted-lists/ 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例 1#xff1a; 输入#xff1a;l1 [1,2,4], l2 [1,3,4] 输出#xff1a;[1,1,2,3,4,4]示例 2…1.题目 https://leetcode.cn/problems/merge-two-sorted-lists/ 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。  示例 1 输入l1 [1,2,4], l2 [1,3,4] 输出[1,1,2,3,4,4]示例 2 输入l1 [], l2 [] 输出[]示例 3 输入l1 [], l2 [0] 输出[0]提示 两个链表的节点数目范围是 [0, 50]-100 Node.val 100l1 和 l2 均按 非递减顺序 排列代码模版 /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/ struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) { } 2.自解 一个容易想到的解法:取小的尾插(开一个新的链表) 对于链表list1和list2,可以另外开一个新的链表,再将list1和list2的val复制进新链表的节点,最后返回新链表的头结点的地址即可 不加思索写出以下代码: /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/ struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {struct ListNode* cur1list1;struct ListNode* cur2list2;if (list1NULL)return list2;if (list2NULL)return list1;struct newListNode{int new_val;struct ListNode* new_next;};struct newListNode* new_nextNULL;struct newListNode* newheadNULL;struct newListNode* m_m_new(struct newListNode*)malloc(sizeof(struct newListNode));newheadm_m_new;newhead-new_nextNULL;struct newListNode* new_curnewhead;while(cur1!NULL cur2!NULL){if (cur1NULL){new_cur-new_valcur2-val;cur2cur2-next;//分配新结点的空间struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;new_curm_new;continue;}if (cur2NULL){new_cur-new_valcur1-val;cur1cur1-next;struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;new_curm_new;continue;}if (cur1-valcur2-val){new_cur-new_valcur1-val;cur1cur1-next;struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;new_curm_new;}else{new_cur-new_valcur2-val;cur2cur2-next;//分配新结点的空间struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;new_curm_new;}}new_cur-new_nextNULL;new_curNULL;return newhead; } 运行时出现问题 发现while循环的条件写错了!! 应该改成 while(!(cur1NULL cur2NULL)) 完整代码 struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {struct ListNode* cur1list1;struct ListNode* cur2list2;if (list1NULL)return list2;if (list2NULL)return list1;struct newListNode{int new_val;struct ListNode* new_next;};struct newListNode* new_nextNULL;struct newListNode* newheadNULL;struct newListNode* m_m_new(struct newListNode*)malloc(sizeof(struct newListNode));newheadm_m_new;newhead-new_nextNULL;struct newListNode* new_curnewhead;struct newListNode* before_new_curNULL;while(!(cur1NULL cur2NULL)){if (cur1NULL){new_cur-new_valcur2-val;cur2cur2-next;struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;before_new_curnew_cur;new_curm_new;new_cur-new_nextNULL;continue;}if (cur2NULL){new_cur-new_valcur1-val;cur1cur1-next;struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;before_new_curnew_cur;new_curm_new;continue;}if (cur1-valcur2-val){new_cur-new_valcur1-val;cur1cur1-next;struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;new_curm_new;}else{new_cur-new_valcur2-val;cur2cur2-next; struct newListNode* m_new(struct newListNode*)malloc(sizeof(struct newListNode));new_cur-new_nextm_new;new_curm_new;}}before_new_cur-new_nextNULL;return newhead; } before_new_cur是当cur1NULL或cur2NULL,备份new_cur的前一个节点的地址 提交结果 3.其他解法 方法1:取小的尾插(不开新链表) struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {struct ListNode* cur1list1;struct ListNode* cur2list2;struct ListNode* headNULL;struct ListNode* tailNULL;if (list1NULL)return list2;if (list2NULL)return list1;while (cur1 cur2){if (cur1-valcur2-val){if (headNULL){headtailcur1;}else{tail-nextcur1;tailtail-next;}cur1cur1-next;}else{if (headNULL){headtailcur2;}else{tail-nextcur2;tailtail-next;}cur2cur2-next; }}if(cur1)tail-nextcur1;if(cur2)tail-nextcur2;return head; } 分析 尾插要有尾指针tail(这样不用频繁找尾),同时要有指向头节点的指针head用于返回 cur1-valcur2-val和cur1-valcur2-val操作方式是类似的
http://www.hkea.cn/news/14285778/

相关文章:

  • 怎样更换动易2006网站模板网站增加权重
  • 网站 谁建设谁负责牧风的在wordpress
  • 建网站得多少钱网络营销软件推广
  • 灯具公司网站模板杭州建设网站 网站建设
  • 外贸公司网站多少钱梅州生态建设有限公司网站
  • 做网站网页的公司宣传信息网网站规划书
  • 在线做网站黄郑州做网站服务器
  • 做公众号一般在哪个网站照片男女的做那个视频网站
  • 1个月能学好网站开发吗手机网站的模板下载
  • 最新被百度收录的网站wordpress域名空间
  • 黄冈网站推广软件下载wordpress转成hexo
  • 网站制作与管理技术标准实训教程扬州做网站的
  • 亚翔建设集团有限公司网站网络营销企业网站设计
  • 宁波做网站的公司哪家好美工培训班学校
  • 怎么编程一个网站返利网站做鹊桥推广
  • 安联建设集团股份公司网站百度官方网
  • 网站建设5000费用预算网站二维码链接怎么做
  • 站长工具网站备案wordpress qq微信登录
  • 做本地团购网站网络规划设计师月薪多少
  • 福州网站设计定制公司建设工程教育网好还是环球网站好
  • 模拟网站效果为网站网站做代理被判缓刑
  • 网站模板网广州企业网站模板建站
  • 在网上建设网站需要花钱么营销型网站怎么做
  • 学校网站的建设论文北京市建设工程网
  • 个人博客网站取名怎么做网站的百度收录
  • 网站建设功能图网页设计制作规范
  • 怎么做付款链接网站怎么做网页商城
  • 江苏省建设厅网站培训网网站流量降低
  • 富阳网站建设洛洛科技大邑县建设银行网站
  • 如何做简单的网站 域名邮箱嘉兴中小企业网站制作