Here is my code for "Merge Two Sorted Lists" algorithm problem on Leetcode:
/**
* 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 *dummy, *pre;
dummy->next = l1;
pre = dummy;
while(l1 != NULL & l2 != NULL) {
if(l1->val < l2->val) {
pre = l1;
l1 = l1->next;
} else {
pre->next = l2;
l2->next = l1;
pre = l2;
l2 = l2->next;
}
}
if(l2 != NULL) {
pre->next = l2;
}
return dummy->next;
}
};
And I got a Runtime Error. But what is wrong with my code?