题解 | #重排链表#

重排链表

https://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * 
 * @param head ListNode类 
 * @return  void
 */

struct ListNode *FindLastNode(struct ListNode *head)
{
    if (head->next == NULL) {
        return head;
    }

    struct ListNode *temp = head->next;
    if (temp->next == NULL) {
        head->next = NULL;
        return temp;
    }

    return FindLastNode(head->next);
}

void reorderList(struct ListNode* head ) {
    // write code here
    struct ListNode *temp = head;
    struct ListNode *lastNode = NULL;

    if ((head == NULL) || (head->next == NULL)) {
        return;
    }

    while (temp != NULL) {
        lastNode = FindLastNode(temp);
        lastNode->next = temp->next;
        if (temp != lastNode) {
            temp->next = lastNode;
        }
        temp = lastNode->next;
    }
}

全部评论

相关推荐

smile丶snow:项目完成时间要写一个大概的区间,自己顺延一下就行。感觉ai对话的放第一个比较好。可以自己编一些场景或者找ai编一个场景。就是你为什么要写这个仿DeepSeek对话应用。比如你自己有很多文档,这个ai可以基于你自己的文档回答之类的。个人建议~具体看你自己。 还有项目中用到那些更好让ai coding的方法也可以写一下,毕竟现在ai大跃进…
简历被挂麻了,求建议
点赞 评论 收藏
分享
不知道怎么取名字_:现在找工作是真的太不容易了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务