今天第一次尝试链表 Linked List ,本以为c#在刷算法的时候有很多局限性,一直纠结要不要换成python刷,如果换成了Python又违背的初心,遂决定系统的学习微软定义的链表方法。通过学习,发现很有必要深入的研究,能够解决很多未来遇到的问题,到那个时候,不会仅仅只使用int 、 string 设计程序。
//定义一个ListNode类
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
public ListNode MergeTwoLists(ListNode l1, ListNode l2)
{
if (l1 == null) return l2;
if (l2 == null) return l1;
ListNode head = (l1.val < l2.val) ? l1 : l2;
ListNode nonhead = (l1.val < l2.val) ? l2 : l1;
head.next = MergeTwoLists(head.next, nonhead);
return head;
}链表提供的类:
https://www.cnblogs.com/afei-24/p/6830160.html
other c#答案:
https://blog.csdn.net/qq_39643935/article/details/78187138
本文分享了作者首次尝试使用C#实现链表的经历,包括定义ListNode类及合并两个有序链表的方法,并附上了相关资源链接。

6614

被折叠的 条评论
为什么被折叠?



