【done】剑指offer——面试题17:合并两个排序的链表

力扣,https://leetcode.cn/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/description/

1.不增加辅助的头结点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode trainningPlan(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode head, cur;
        if (l1.val <= l2.val) {
            head = l1;
            cur = head;
            l1 = l1.next;
        } else {
            head = l2;
            cur = head;
            l2 = l2.next;
        }

        while (l1 != null || l2 != null) {
            if (l1 == null) {
                cur.next = l2;
                l2 = l2.next;
            } else if (l2 == null) {
                cur.next = l1;
                l1 = l1.next;
            } else if (l1.val <= l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else if (l1.val > l2.val) {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }

        return head;
    }
}

2.增加辅助的头结点

class Solution {
    public ListNode trainningPlan(ListNode l1, ListNode l2) {
        ListNode preHead = new ListNode(-1);
        ListNode cur = preHead;

        while (l1 != null || l2 != null) {
            if (l1 == null) {
                cur.next = l2;
                break;
            } else if (l2 == null) {
                cur.next = l1;
                break;
            } else if (l1.val <= l2.val) {
                cur.next = l1;
                l1 = l1.next;
            } else if (l1.val > l2.val) {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }

        return preHead.next;
    }
}

Solution1:
不要犯低级错误。。。

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2){
        if(pHead1 == NULL)
            return pHead2;
        else if(pHead2 == NULL)
            return pHead1;
        else{
            struct ListNode* ListMergeHead,*ListMergeTail,*temp1 = pHead1,*temp2 = pHead2;
            if(temp1->val <= temp2->val){ //找到头指针
                ListMergeHead = temp1;
                temp1 = temp1->next;
            }
            else {
                ListMergeHead = temp2;
                temp2 = temp2->next;
            }
            ListMergeTail=ListMergeHead;
            while(temp1 != NULL && temp2 != NULL){
                if(temp1->val <= temp2->val){
                    ListMergeTail->next = temp1;
                    ListMergeTail = ListMergeTail->next;
                    temp1 = temp1->next;
                }
                else{
                    ListMergeTail->next = temp2;
                    ListMergeTail = ListMergeTail->next;
                    temp2 = temp2->next;
                }
            }
            if(temp1 == NULL)
                ListMergeTail->next = temp2;
            else
                ListMergeTail->next = temp1;
            return ListMergeHead;
        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值