7-7 两个有序链表序列的合并 (20 分)

本文介绍了一种方法,用于合并两个非降序链表序列S1与S2,生成新的非降序链表S3。通过将两个链表的所有元素读入数组,使用sort()函数进行排序,实现了链表的高效合并。

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2合并后的新的非降序链表S3。

输入格式:

输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:

在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL

输入样例:

1 3 5 -1
2 4 6 8 10 -1

输出样例:

1 2 3 4 5 6 8 10

这题输入时就可以合并了,然后用sort排序就好。

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int long long N=10e7;
int a[N],b[N];
int main()
{
	long long ch,k=0;
	while(cin>>ch&&ch!=-1)
	{
		a[k]=ch;
		k++;
	}
	while(cin>>ch&&ch!=-1)
	{
			
		a[k]=ch;
		k++;
	}
	sort(a,a+k);
	if(k==0)
		cout<<"NULL";
	else
		for(long long i=0;i<k;i++)
		{
			cout<<a[i];
			if(i!=k-1)
			cout<<" "; 
		}
	return 0;		
}

 

以下是几种使用 Python 实现两个有序链表序列合并的代码和方法: ### 列表融合方法 ```python class Node: def __init__(self, value): self.value = value self.next = None class LinkedList: def __init__(self, root=None): self.root = root @classmethod def from_list(cls, values): dummy = Node(0) current = dummy for value in values: current.next = Node(value) current = current.next return cls(dummy.next) def __str__(self): result = [] current = self.root while current: result.append(str(current.value)) current = current.next return " ".join(result) def merge_linked_lists(list1, list2): # 创建一个哑节点作为合并链表的头节点 cur = pre_root = Node(0) list1_node, list2_node = list1.root, list2.root # 迭代比较两个链表的节点值,将较小的节点连接到合并后的链表中 while list1_node is not None and list2_node is not None: if list1_node.value <= list2_node.value: cur.next = list1_node list1_node = list1_node.next else: cur.next = list2_node list2_node = list2_node.next cur = cur.next # 将剩余的节点连接到合并后的链表中 if list1_node is not None: cur.next = list1_node if list2_node is not None: cur.next = list2_node return LinkedList(root=pre_root.next) l1 = LinkedList.from_list([1, 2, 4]) l2 = LinkedList.from_list([1, 3, 4, 5]) l3 = merge_linked_lists(l1, l2) print(l3) ``` 此方法通过创建一个哑节点作为合并链表的头节点,迭代比较两个链表的节点值,将较小的节点连接到合并后的链表中,最后将剩余的节点连接到合并后的链表末尾 [^2]。 ### 标准求解【直接合并】 ```python # Definition for singly-linked list. class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def mergeTwoLists(self, list1, list2): res = ListNode() ans = res while list1 != None and list2 != None: if list1.val <= list2.val: ans.next = list1 list1 = list1.next else: ans.next = list2 list2 = list2.next ans = ans.next if list1 == None: ans.next = list2 else: ans.next = list1 return res.next ``` 该方法同样是通过迭代比较两个链表节点值,将较小值的节点依次连接起来,最后处理剩余节点 [^4]。 ### 列表合并法(PTA 相关) ```python list1 = [int(x) for x in input().split() if x != '-1'] list2 = [int(s) for s in input().split() if s != '-1'] list_merged = list1 + list2 list_merged.sort() if len(list_merged) == 0: print('NULL') else: for i in list_merged[:-1]: print(i, end=' ') print(list_merged[-1]) ``` 此方法先将输入的两个有序链表数据别存储为列表,合并两个列表后进行排序,再按要求输出结果 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值