Skip to content

#滴滴面试题 #293

@MyLinChi

Description

@MyLinChi

实现10个线程并发执行,完成后返回结果

求解

使用CountDownLatch实现,每一个线程的run方法运行完毕调用一次countDown方法,然后在主线程中执行await方法等待所有子线程的执行结束。

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

class Solution {
public:
    /**
     * 逆序
     * @param head ListNode类 头结点
     * @return ListNode类
     */
    ListNode* reverseList(ListNode* head) {
        // write code here
        ListNode tmp(0),*res = &tmp;
        for(ListNode* p = head->next;p;){
            ListNode* tp = p->next;
            p->next = res->next;
            res->next = p;
            p = tp;
        }
        return res->next;
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions