Leetcode 21. Merge Two Sorted Lists | | Leetcode 21. Merge Two Sorted Lists 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution1234567891011121314151617181920212223242526272829303132333435/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* ptrFirst = l1; ListNode* ptrSecond = l2; ListNode* head = new ListNode(0); ListNode* current = head; while(ptrFirst && ptrSecond) { if(ptrFirst->val > ptrSecond->val) { current->next = ptrSecond; ptrSecond = ptrSecond->next; } else { current->next = ptrFirst; ptrFirst = ptrFirst->next; } current = current->next; } if(ptrFirst) { current->next = ptrFirst; } if(ptrSecond) { current->next = ptrSecond; } return head->next; }}; Reference https://leetcode.com/problems/merge-two-sorted-lists/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏