【单链表OJ题:合并两个有序链表】

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
题目来源

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){

    if(list1 == NULL)
        return list2;
    if(list2 == NULL)
        return list1;

    struct ListNode* head = NULL;
    struct ListNode* end = NULL;

    while(list1 && list2){
        if(list1->val<list2->val){
            if(end == NULL){
                head = end = list1;
            }else{
                end->next = list1;
                end = end->next;
            }
            list1 = list1->next;
            }else{
            if(end == NULL){
                head = end = list2;
            }else{
                end->next = list2;
                end = end->next;            
            }
            list2 = list2->next;
        }
    }
    if(list1){
        end->next = list1;
    }
    if(list2){
        end->next = list2;
    }
    return head;
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    //合并两个有序的链表
    struct ListNode* s1 = list1;
    struct ListNode* s2 = list2;
    if(s1 == NULL)
        return s2;
    
    if(s2 == NULL)
        return s1;
    
    struct ListNode* head = NULL;
    struct ListNode* end = NULL;

    while(s1 && s2){
        if(s1->val<s2->val){
            if(end == NULL){
                head = end = s1;
            }else{
                end->next = s1;
                end = end->next;
            }
            s1 = s1->next;        
        }else{
            if(end == NULL){
                head = end = s2;
            }else{
                end->next = s2;
                end = end->next;
            }
            s2 = s2->next;
        }
    }
    if(s1){
        end->next = s1;
    }
    if(s2){
        end->next = s2;
    }
    return head;
}

带头结点的解法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    if(list1 == NULL)
        return list2;    
    if(list2 == NULL)
        return list1;
    
    struct ListNode* head = NULL;
    struct ListNode* end = NULL;
	带一个头结点 方便尾插
    head = end = (struct ListNode*)malloc(sizeof(struct ListNode));

    while(list1 && list2){
        if(list1->val<list2->val){
            end->next = list1;
            end = end->next;
            list1 = list1->next;        
        }else{
            end->next = list2;
            end = end->next;
            list2 = list2->next;
        }
    }
    if(list1){
        end->next = list1;
    }
    if(list2){
        end->next = list2;
    }
    struct ListNode* del = head;
    head = head->next;
    free(del);

    return head;
}

你可能感兴趣的:(链表,linux,数据结构)