148. 排序链表
最近更新:2025-03-26 | 字数总计:114 | 阅读估时:1分钟 | 阅读量:次
- 解析
Problem: 148. 排序链表
解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class Solution { public: ListNode *sortList(ListNode *head) { } ListNode *mergeList(ListNode *head1, ListNode *head2) { ListNode *node1 = head1; ListNode *node2 = head2; ListNode *dummy = new ListNode(); ListNode *node = dummy; while (node1 != nullptr && node2 != nullptr) { if (node1->val <= node2->val) { node->next = node1; node = node->next; node1 = node1->next; } else { node->next = node2; node = node->next; node2 = node2->next; } } while (node1 != nullptr) { node->next = node1; node = node->next; node1 = node1->next; } while (node2 != nullptr) { node->next = node2; node = node->next; node2 = node2->next; } ListNode *res = dummy->next; delete dummy; return res; } };
|