Existing spectrum management policies have led to significant overallocation and underutilization of the licensed spectrum. To address this issue, cognitive radio (CR) has been proposed as a promising new technology. In particular, it can be utilized to enable secondary users (SUs) to share the licensed spectrum if they do not cause harmful interference to primary users. As such, the transmission power of an SU should be limited, even when it accesses spectrum holes. Therefore, multihop transmission is a potential method to deliver the data of SUs over long distances. However, due to unstable spectrum opportunity, heterogeneous spectrum environment, and limited signaling overhead, how to achieve reliable and efficient transmission is a challenging issue. In this paper, we first propose an agile rateless coded relaying (ARCOR) scheme for CRs, where discontinuous spectrum holes in three dimensions can be aggregated by employing rateless codes under a heterogeneous spectrum environment. An additional degree of freedom is also provided by utilizing rateless codes to set the number of decoding relays, which can be used to balance the first and second hops. Without assuming perfect spectrum sensing, we analyze and optimize the end-to-end throughput with the presence of sensing errors. Furthermore, a block-search-based algorithm is proposed to find the optimal number of decoding relays with low complexity. Simulation results will show that the optimized ARCOR scheme can achieve high spectrum efficiency and combat imperfect sensing results in CRs.