Is my merge list right?
-
struct Node { int data; Node *next; }; typedef struct Node Node; Node *Reverse(Node *head); Node *Merge(Node *head1, Node *head2) { if (head1 == NULL) return head2; if (head2 == NULL) return head1; Node *head = NULL; Node *p1 == NULL; Node *p2 == NULL; if (head1->data > head2->data) { head->next = head1; p1 = head1->next; p2 = head2; } else { head->next = head2; p1 = head1; p2 = head2 -> data; } // I want to see is that why some book in this step using " Node *pcurrent =head" //and I dont not know why,coould someone give me a detail, thanks in advantage! while (p1!=NULL && p2!=NULL) { if (p1->data > p2->data) { head->next =p1; head = p1; p1 = p1->next; } else { head -next = p2; head = p2; p2 = p2->next; } } while (p1 != NULL) { head->next = p1; head = p1; p1 = p2->next; } while (p2 != NULL) { head -next = p2; head = p2; p2 = p2->next; } return head; }
-
struct Node { int data; Node *next; }; typedef struct Node Node; Node *Reverse(Node *head); Node *Merge(Node *head1, Node *head2) { if (head1 == NULL) return head2; if (head2 == NULL) return head1; Node *head = NULL; Node *p1 == NULL; Node *p2 == NULL; if (head1->data > head2->data) { head->next = head1; p1 = head1->next; p2 = head2; } else { head->next = head2; p1 = head1; p2 = head2 -> data; } // I want to see is that why some book in this step using " Node *pcurrent =head" //and I dont not know why,coould someone give me a detail, thanks in advantage! while (p1!=NULL && p2!=NULL) { if (p1->data > p2->data) { head->next =p1; head = p1; p1 = p1->next; } else { head -next = p2; head = p2; p2 = p2->next; } } while (p1 != NULL) { head->next = p1; head = p1; p1 = p2->next; } while (p2 != NULL) { head -next = p2; head = p2; p2 = p2->next; } return head; }
Hi, please edit your post and add: 1. PRE tags instead of CODE tags, for better readability; 2. a textual description of your linked lists; e.g. is head supposed to contain any data, or is it only pointing to the first real node? 3. a textual description of what Merge is supposed to do (which must include preconditions: what is assumed about its parameters, and what is promissed about its return value). Without it, the only thing anyone can say is "if it compiles, it will do what it will do". :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]