#include <stdio.h>
#include <stdlib.h>
//definition for the singly-linked list.
struct ListNode{
int val;
struct ListNode *next;
};
//convert an array of integer to a singly-linked list.
void build(struct ListNode* head, int* nums, int numsSize){
struct ListNode *p, *q;
p = head;
for(int i=0; i<numsSize; ++i){
q = (struct ListNode*)malloc(sizeof(struct ListNode));
q->next = NULL;
q->val = nums[i];
p->next = q;
p = q;
}
}
//print the given singly-linked list.
void print(const struct ListNode* head){
struct ListNode* p = head->next;
while(p != NULL){
printf("%d ", p->val);
p = p->next;
}
}
int main()
{
struct ListNode* L = (struct ListNode*)malloc(sizeof(struct ListNode));
L->next = NULL;
L->val = -1;
int A[] = {1,2,3,4,5,6};
build(L, A, 6);
print(L);
return 0;
}
单链表
最新推荐文章于 2023-05-06 22:43:40 发布