标签:struct str null 合并 else pre code amp new
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 //思路很清晰,建立一个虚拟节点 10 class Solution 11 { 12 public: 13 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) 14 { 15 ListNode* dummy = new ListNode(-1); 16 ListNode* head = dummy; 17 while(l1 && l2) 18 { 19 if(l1->val < l2->val) 20 { 21 head->next = l1; 22 l1 = l1->next; 23 } 24 else 25 { 26 head->next = l2; 27 l2 = l2->next; 28 } 29 head = head->next; 30 } 31 head->next = l1 ? l1 : l2; 32 return dummy->next; 33 } 34 };
标签:struct str null 合并 else pre code amp new
原文地址:https://www.cnblogs.com/yuhong1103/p/12499207.html