First look at the implementation of the Code:
#include <stdio.h> #include <stdlib.h>//list storage structure typedef struct NODE {int number;
struct Node * NEXT;
}node;
typedef struct NODE * linklist;
void Createlisthead (linklist * phead, int option)//parameter phead pointer to pointer {linklist p, r;
int i; *phead = (linklist) malloc (sizeof (Node));
Head node, no data R = *phead;
if (option = 1)//Generate odd incrementing list {for (i = 1; i < i + + 2) {p = (linklist) malloc (Node);
P->number = i;
R->next = p;
r = P; } else if (option = 2)//Generate even increment list {for (i = 2; i < i + + 2) {p = (linklist) malloc (Node
));
P->number = i;
R->next = p;
r = P;
} r->next = NULL;
} void Displayelem (Linklist phead) {while (Phead) {printf ("%d\t", Phead->number);
Phead = phead->next;
printf ("\ n");
} linklist mergedlist (linklist pHead1, linklist pHead2) {linklist phead = NULL;
if (PHead1 = = NULL) {return pHead2; else if (pHead2 = NULL) {return PHead1;
} if (Phead1->number < phead2->number) {phead = PHead1;
Phead->next = Mergedlist (Phead1->next, pHead2);
else {phead = pHead2;
Phead->next = Mergedlist (PHead1, Phead2->next);
return phead;
int main () {linklist pHead1 = NULL;
linklist pHead2 = NULL;
linklist phead = NULL; Createlisthead (&phead1, 1); /* The address of the incoming pointer itself, where readers have to think about why this is equivalent to the address of the PHEAD1 pointer itself, and then manipulate the finger in the *createlisthead function
Needle PHead1, let it have a pointing * * * Createlisthead (&PHEAD2, 2);
Displayelem (Phead1->next); Displayelem (Phead2->next);
PHead1 and PHead2 are two sorted lists//below to combine the two sorted lists into an ordered ascending list phead = Mergedlist (Phead1->next, Phead2->next);
Displayelem (Phead);
return 0; }
Program Run display results:
Two sequentially incremented list pHead1 and pHead2, if the value of the first node of PHEAD1 is greater than the value of the first node of pHead2, the PHEAD1 header node is assigned to Phead. Then make the next comparison, and vice versa is pHead2 to phead. As shown in figure:
This is a recursive process, when the last node of the linked list, after the end of recursion, then return the nodes of each recursive function, and finally combined into a linked list-the merged sequential list.
About the list of knowledge, pro can go to see "Big liar data Structure" Oh.
When I was interviewing a Java position in a company, I was asked to write this algorithm in a recursive way, looking at Java code:
public class Mergesortedlinkedlist {public class Node {private int number;
Private Node Next;
node (int number, Node next) {this.number = number;
This.next = Next; }/** * Returns the header node * @param isodd * @return/Public node Createsortedlinkedlist (the Boolean is
ODD) {Node p, head;
Header node P = head = new Node (0, NULL); if (isodd) {//create odd ordered list for (int i = 1; i < i + + 2) {node node = new node
(I, NULL);
P.next = node;
p = node; else {//Create an even ordered list for (int i = 2; i < i + 2) {node node = NE
W Node (i, NULL);
P.next = node;
p = node;
} return head; Public node Mergetwosortedlinkedlist (node Head1, node head2) {//note parameter judgment, the interviewer also values the IF (null = = Head1) {return head2;
} if (null = = head2) {return head1;
///To set the value node p for the new merge head node;
if (Head1.number < Head2.number) {head = Head1;
Head1 = Head1.next;
else {head = head2;
Head2 = Head2.next;
} p = head; while (null!= head1 && null!= head2) {if (Head1.number < Head2.number) {P.next
= Head1;
The P pointer moves down to the new node p = head1;
Head1 = Head1.next;
else {p.next = head2;
p = head2;
Head2 = Head2.next;
The processing of the remaining nodes if (null!= head1) {p.next = Head1;
} if (null!= head2) {p.next = head2;
return head;
public void Display (node node) {while (null!= Node) { System.out.print (Node.number + "");
node = Node.next;
} System.out.println (); public static void Main (string[] args) {mergesortedlinkedlist sortedlinkedlist = new Mergesortedlinkedlist
();
Node Head1 = Sortedlinkedlist.createsortedlinkedlist (true);
Node head2 = Sortedlinkedlist.createsortedlinkedlist (false);
System.out.print ("Linked list one:");
Sortedlinkedlist.display (Head1.next);
System.out.print ("linked list two:");
Sortedlinkedlist.display (Head2.next);
System.out.print ("merged linked List:");
Sortedlinkedlist.display (Sortedlinkedlist.mergetwosortedlinkedlist (Head1.next, Head2.next)); }
}