剑指offer (16)

题目 : 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

思路 : 简单题 ,不做赘述 ,看代码注释

	public ListNode Merge(ListNode list1, ListNode list2) {
		ListNode pre3 = head;
		ListNode preRet = pre3;
		while (list1 != null) {
			if (list2 == null) {
				pre3.next = list1;
				return preRet.next;
			} 
			else if (list1.value < list2.value) {
				pre3.next = new ListNode(list1.value);
				pre3 = pre3.next;
				list1 = list1.next;
			}
			else {
				pre3.next = new ListNode(list2.value);
				pre3 = pre3.next;
				list2 = list2.next;
			}
		}
		pre3.next = list2;
		return preRet.next;
	}
发布了50 篇原创文章 · 获赞 0 · 访问量 411

猜你喜欢

转载自blog.csdn.net/weixin_46108108/article/details/104198349
今日推荐