21.Merge Two Sorted Lists(法1头部附加节点法2二级指针)

Merge two sorted linked lists and return it as a new list. The new listshould be made by splicing together the nodes of the first two lists.

HideTags

Linked List

#pragma once
#include<iostream>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

//法1:头部添加冗余指针,正常merge
ListNode *mergeTwoLists1(ListNode *l1, ListNode *l2)
{
	ListNode* result = new ListNode(-1);//头部附加节点,应返回result->next
	ListNode* p = result;
	while (l1&&l2)
	{
		if (l1->val < l2->val)
		{
			p->next = new ListNode(l1->val);//注意不能写成是p=new。。。。
			l1 = l1->next;
		}
		else
		{
			p->next = new ListNode(l2->val);
			l2 = l2->next;
		}
		p = p->next;
	}
	while (l1)
	{
		p->next= new ListNode(l1->val);
		l1 = l1->next;
		p = p->next;
	}
	while (l2)
	{
		p->next = new ListNode(l2->val);
		l2 = l2->next;
		p = p->next;
	}
	return result->next;
}

//法2:二级指针
ListNode *mergeTwoLists2(ListNode *l1, ListNode *l2)
{
	ListNode *result;
	ListNode **pp = &result;
	while (l1&&l2)
	{
		if (l1->val < l2->val)
		{
			*pp = new ListNode(l1->val);
			l1 = l1->next;
		}
		else
		{
			*pp = new ListNode(l2->val);
			l2 = l2->next;
		}
		pp = &(*pp)->next;
	}
	while (l1)
	{
		*pp = new ListNode(l1->val);
		l1 = l1->next;
		pp = &(*pp)->next;
	}
	while (l2)
	{
		*pp = new ListNode(l2->val);
		l2 = l2->next;
		pp = &(*pp)->next;
	}
	return result;
}
void main()
{
	ListNode* l1 = new ListNode(1);
	ListNode* l2 = new ListNode(2);
	ListNode* l3 = new ListNode(1);
	ListNode* l4 = new ListNode(1);

	ListNode* result = mergeTwoLists2(l2, l1);
	while (result)
	{
		cout << result->val << ' ';
		result = result->next;
	}
	cout << endl;
	system("pause");
}
时间: 2024-08-01 16:56:11

21.Merge Two Sorted Lists(法1头部附加节点法2二级指针)的相关文章

leetCode 21. Merge Two Sorted Lists 合并链表

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目大意:合并两个有序的链表 思路:通过比较两个链表的节点大小,采用尾插法建立链表. 代码如下: /**  * Definition for singly-lin

21. Merge Two Sorted Lists(js)

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4题意

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example Example: Input: 1->2->4, 1-&g

leedCode练题——21. Merge Two Sorted Lists(照搬大神做法)

1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4-&g

[Leetcode][Python]21: Merge Two Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 21: Merge Two Sorted Listshttps://oj.leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as a new list.The new list should be made by splicing together th

C# 写 LeetCode easy #21 Merge Two Sorted Lists

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

LeetCode 21 Merge Two Sorted Lists

翻译 合并两个排好序的链表,并返回这个新链表. 新链表应该由这两个链表的头部拼接而成. 原文 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 代码 /** * Definition for singly-linked list. * struct ListNode

LeetCode记录之21——Merge Two Sorted Lists

算法和数据结构这东西,真的是需要常用常练.这道看似简单的链表合并题,难了我好几个小时,最后还是上网搜索了一种不错算法.后期复习完链表的知识我会将我自己的实现代理贴上. 这个算法巧就巧在用了递归的思想,按照常规方法也能求得,但是就未免太复杂了. Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the fir