1/**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * struct ListNode *next;
6 * };
7 */
8
9 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
10 struct ListNode *p1=l1,*p2=l2,*r=l1,*s;
11
12 while(p1 && p2){
13 if( p1->val <= p2->val ){
14 s = p1->next;
15 r->next = p1;
16 r = p1;
17 p1 = s;
18 }
19 else{
20 s = p2->next;
21 r->next = p2;
22 r = p2;
23 p2 = s;
24 }
25 }
26
27 if(p1) p2 = p1;
28 while(p2){
29 s = p2->next;
30 r->next = p2;
31 printf("%d",r);
32 r = p2;
33 p2 = s;
34 }
35 return l1;
36}
结果正常,但是提交就会显示错误Line 30: Char 17: runtime error: member access within null pointer of type 'struct ListNode' (solution.c)
懂了,空表的可能,这个点确实没想到。真是个精细活。