[Linked list OJ—merging two ordered linked lists]

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

世上有两种耀眼的光芒,一种是正在升起的太阳,一种是正在努力学习编程的你!一个爱学编程的人。各位看官,我衷心的希望这篇博客能对你们有所帮助,同时也希望各位看官能对我的文章给与点评,希望我们能够携手共同促进进步,在编程的道路上越走越远!


提示:以下是本篇文章正文内容,下面案例可供参考

1. Question on merging two ordered linked lists:

Solution one:

Problem-solving ideas:

1. There are sentinel bits:Create two pointer variables cur1 and cur2: used to replace the head nodes of the original linked lists list1 and list2 respectively, and traverse backward; create a pointer variable with An empty linked list of sentinel bits (used to tail insert the smaller value compared to the original linked list), and create two pointer variables newhead and newtail that jointly point to the sentinel bits. Because there are sentinel bits, the head node of the new linked list has an address, so the original linked list is compared. Tail insertion of smaller values ​​in the linked list does not need to consider whether the linked list is empty.

2. No sentinel bit: Create two pointer variables cur1 and cur2: used to replace the head nodes of the original linked lists list1 and list2 respectively, and traverse backward; create an empty linked list , create two pointer variables newhead and newtail that both point to NULL. Because the first node may be empty, when inserting the smaller value in the original linked list, you need to consider whether the linked list is empty.

Code:


Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/134895452