結構_奇偶前後_我不會

 avatar
user_3763047219
c_cpp
a year ago
1.4 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
    int val;
    struct ListNode* next;
};

struct ListNode* oddEvenList(struct ListNode* head);



struct ListNode* oddEvenList(struct ListNode* head)
{
    struct ListNode* head2 = head;
    struct ListNode* ans=head;
    struct ListNode* ptr;
    ptr = ans->next;
    while (head->next->next != NULL) {
        ptr = head->next->next;
        ptr++;
        head = head->next->next;
    }
    while (head2->next != NULL) {
        ptr = head2->next;
        ptr++;
        head2 = head2->next;
    }
    return ans;
}



void Construct(struct ListNode* node, int length, int num)
{
    int a;
    scanf("%d", &a);
    node->val = a;

    if (num >= length)
    {
        node->next = NULL;
        return;
    }
    node->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    num++;
    Construct(node->next, length, num);
}

int main()
{
    struct ListNode* head;
    int n, a, length;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    scanf("%d", &n);
    while (n--)
    {
        scanf("%d", &length);
        Construct(head, length, 1);
        struct ListNode* ans = oddEvenList(head);
        struct ListNode* ptr;

        ptr = ans;

        while (ptr != NULL)
        {
            printf("%d ", ptr->val);
            ptr = ptr->next;
        }
        printf("\n");
    }
}