鱼C论坛

 找回密码
 立即注册
查看: 1713|回复: 0

[技术交流] C++刷剑指offer(面试题25. 合并两个排序的链表)【链表】

[复制链接]
发表于 2020-3-29 12:27:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-5-8 17:31 编辑

题目描述:

  1. 输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。

  2. 示例1:

  3. 输入:1->2->4, 1->3->4
  4. 输出:1->1->2->3->4->4
  5. 限制:

  6. 0 <= 链表长度 <= 1000
复制代码


  1. #include <iostream>


  2. using namespace std;

  3. struct ListNode{
  4.         int value;
  5.         ListNode* next;
  6.         ListNode(int x): value(x), next(NULL){
  7.                
  8.         }
  9. };

  10. void printList(ListNode* input){
  11.         ListNode* temp = input;
  12.         while(temp -> next){
  13.                 temp = temp -> next;
  14.                 cout << temp -> value << " ";       
  15.         }
  16.         cout << endl;
  17.         cout << "-------------" << endl;
  18.        
  19. }


  20. ListNode* solution(ListNode* input1, ListNode* input2){
  21.         ListNode* temp1 = input1 -> next;
  22.         ListNode* temp2 = input2 -> next;
  23.         ListNode* temp = new ListNode(0);
  24.         ListNode* res = temp;
  25.         while(temp1 != NULL || temp2 != NULL){
  26.                 if(temp1 == NULL){
  27.                         temp -> next = temp2;
  28.                         break;
  29.                 }
  30.                 if(temp2 == NULL){
  31.                         temp -> next = temp1;
  32.                         break;
  33.                 }
  34.                 if(temp1 -> value <=  temp2 -> value){
  35.                         temp -> next = temp1;
  36.                         temp = temp -> next;
  37.                         temp1 = temp1 -> next;
  38.                         continue;
  39.                 }
  40.                 if(temp1 -> value > temp2 -> value){
  41.                         temp -> next = temp2;
  42.                         temp = temp -> next;
  43.                         temp2 = temp2 -> next;
  44.                         continue;
  45.                 }

  46.                
  47.         }
  48.         return res;
  49.        
  50. }


  51. int main(void){
  52.         ListNode* input1 = new ListNode(0);
  53.         ListNode* temp1 = input1;
  54.         cout << "please send numbers for the first singleList:" << endl;
  55.         int number1;
  56.         while(cin >> number1){
  57.                 ListNode* node = new ListNode(number1);
  58.                 temp1 -> next = node;
  59.                 temp1 = node;
  60.         }
  61.         printList(input1);
  62.         cin.clear();
  63.        
  64.         ListNode* input2 = new ListNode(0);
  65.         ListNode* temp2 = input2;
  66.         cout << "please send numbers for the second singleList:" << endl;
  67.         int number2;
  68.         while(cin >> number2){
  69.                 ListNode* node = new ListNode(number2);
  70.                 temp2 -> next = node;
  71.                 temp2 = node;
  72.         }
  73.         printList(input2);
  74.         cin.clear();
  75.         cout << "start!" << endl;
  76.        
  77.        
  78.         ListNode* res = solution(input1, input2);
  79.         cout << "finish!" << endl;
  80.         printList(res);
  81.        
  82.        
  83.         return 0;
  84. }
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-4 14:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表