gpt4 book ai didi

c# - 如何合并两个排序的链表?

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:57 25 4
gpt4 key购买 nike

这是一个面试问题,但如果有最佳解决方案,我不会。

问题:编写一个函数(用 C# 或 C++)合并两个已经排序的链表。给定一个数据结构:C++:

class Node
{
public:
int data;
Node* next;
};

C#:

class Node
{
public int data;
public Node next;
};

实现函数:在 C++ 中:

Node* Merge (Node* head1, Node* head2)
{

}

在 C# 中:

Node Merge (Node head1, Node head2)
{

}

它接受两个已经排序的链表(按升序),并应该将它们合并成一个排序的链表(按升序)并返回新的头。这 2 个列表可能具有具有相同数据(int 值)的节点。我们希望结果列表没有相同的数据。

我的解决方案:

 Node Merge(Node head1, Node head2)
{
Node merged = head1;
// Both lists are empty
if (head1 == null && head2 == null)
{
return null;
}
// List 1 is empty
else if (head1 == null && head2 != null)
{
return head2;
}
// List 2 is empty
else if (head2 == null && head1 != null)
{
return head1;
}
// Both lists are not empty
else
{
Node cursor1 = head1;
Node cursor2 = head2;

if (cursor1.next.data > cursor2.data)
{
Node temp = cursor1;
cursor1 = cursor2;
cursor2 = temp;
}

// Add all elements from list 2 to list 1
while (cursor1.next != null && cursor2 != null)
{
if (cursor1.next.data < cursor2.data)
{
cursor1 = cursor1.next;
}
else
{
Node temp1 = cursor1.next;
Node temp2 = cursor2.next;
cursor1.next = cursor2;
cursor2.next = temp1;

cursor1 = cursor1.next;
cursor2 = temp2;
}
}

if (cursor1.next == null)
{
cursor1.next = cursor2;
}
}

// Remove duplicates
head1 = merged;
while (head1.next != null)
{
if (head1.data < head1.next.data)
{
head1 = head1.next;
}
else if (head1.data == head1.next.data)
{
head1.next = head1.next.next;
}
}
return merged;
}

请提供一些意见,让我知道您的聪明好解决方案。谢谢!

最佳答案

在确保允许我使用 C# 附带的框架后,这就是我会做的。

给定这个链表类(与你的相同,但使用属性)

class Node
{
public int Data { get; set; }
public Node Next { get; set; }
}

创建一个 IEnumerator对于列表

class NodeEnumerator : IEnumerator<int>
{
private Node _current_node;

public NodeEnumerator(Node first) {
_current_node = new Node { Data = 0, Next = first };
}

public int Current {
get { return _current_node.Data; }
}

object IEnumerator.Current {
get { return Current; }
}

public bool MoveNext() {
if (_current_node == null) {
return false;
}
_current_node = _current_node.Next;
return _current_node != null;
}

public void Reset() {
throw new NotSupportedException();
}

public void Dispose() {

}
}

创建对应的IEnumerable

class EnumerableNode : IEnumerable<int>
{
private Node _first;
public EnumerableNode(Node first) {
_first = first;
}
public IEnumerator<int> GetEnumerator() {
return new NodeEnumerator(_first);
}

IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
}

然后使用我可用的很棒的功能来换取这一点努力,即 ConcatDistinct

class Program
{
static void Main(string[] args) {
var list1 = new EnumerableNode(
new Node { Data = 1, Next =
new Node { Data = 2, Next =
new Node { Data = 3, Next = null }}});
var list2 = new EnumerableNode(
new Node { Data = 2, Next =
new Node { Data = 3, Next =
new Node { Data = 4, Next = null }}});
var merged = list1.Concat(list2).Distinct();
Console.WriteLine(String.Join(",", list1));
Console.WriteLine(String.Join(",", list2));
Console.WriteLine(String.Join(",", merged));
Console.ReadLine();
}
}

输出

1,2,3
2,3,4
1,2,3,4

面试官可能是在寻找算法(比如你的解决方案),但我认为这个更逼真,更优雅,并且仍然显示出解决问题的能力,以及对框架和枚举器的一般概念的了解.它在 Java、Python 和 Ruby(以及我敢肯定的许多其他语言)中的工作方式完全相同。不过,我不知道它将如何转换为 C++。

关于c# - 如何合并两个排序的链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14982640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com