我在合併2列表中有問題,所以我(想)在做什麼。將列表A分配給當前,去往當前節點,將列表B添加到當前,給head2分配電流,最後打印。但是當我運行它沒有一出來,HEAD2列表中仍然空合併兩個鏈表
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int val;
struct node * next;
} node_t;
void print_list(node_t * head);
node_t* merge(node_t *head,node_t *head1);
int main(){
int number;
node_t * head = NULL;
node_t * head1 = NULL;
//inputA
//inputB
printf("merged list is : \n");
print_list(merge(head,head1));
printf("\n");
return 0;
}
node_t *merge(node_t *head,node_t *head1){
node_t * current = head;
while(current->next != NULL){
current = current->next;
}
current->next = head1;
return current;
}
void print_list(node_t * head) {
node_t * current = head;
while (current != NULL) {
printf("%d ", current->val);
current = current->next;
}
}
編輯:頭部名單A和頭像1是名單B,已經有一些數據。它只會保留A ex的最後一個節點。 A = 1 2 3,B = 4 5 6返回3個4 5 6
我建議'HEAD2 =合併(頭,頭像1); ..''在merge':'返回頭;' – BLUEPIXY
我的方法是錯誤的,我如何保持雙方名單? – Hary
創建結果的新列表(每個元素)。 – BLUEPIXY