#include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // 合并两个有序链表并去重的函数 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(0); ListNode* current = dummy; while (l1 && l2) { if (l1->val < l2->val) { if (current->val!= l1->val) { current->next = l1; current = current->next; } l1 = l1->next; } else { if (current->val!= l2->val) { current->next = l2; current = current->next; } l2 = l2->next; } } while (l1) { if (current->val!= l1->val) { current->next = l1; current = current->next; } l1 = l1->next; } while (l2) { if (current->val!= l2->val) { current->next = l2; current = current->next; } l2 = l2->next; } return dummy->next; }
点赞 评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
03-18 14:29
牛客604067584号:感觉算法卷的人少很多,毕竟只有一部分bg还不错的硕士才会考虑算法,虽然hc不如后端,但是竞争真的少很多。
点赞 评论 收藏
分享
牛客网
牛客企业服务