【LeetCode】 86. Partition List C语言

来源:互联网 发布:天津基础教研网络平台 编辑:程序博客网 时间:2024/06/15 01:02

LeetCode解题心得,欢迎交流! 第三日 

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* partition(struct ListNode* head, int x) {    struct ListNode *dummy1=(struct ListNode *)malloc(sizeof(struct ListNode));    struct ListNode *dummy2=(struct ListNode *)malloc(sizeof(struct ListNode));        struct ListNode *cur1=dummy1;    struct ListNode *cur2=dummy2;    while(head!=NULL)    {        if(head->val < x)        {            cur1->next=head;            cur1=head;        }        else        {            cur2->next=head;            cur2=head;        }        head=head->next;    }        cur2->next=NULL;    cur1->next=dummy2->next;    return dummy1->next;}


0 0
原创粉丝点击