题解 | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

递归法----我不理解,但我大受震撼

 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */

/**
 * 
 * @param pHead ListNode类 
 * @return ListNode类
 */


struct ListNode* ReverseList(struct ListNode* pHead ) {
    // write code here
    if((pHead->next == NULL) | (pHead == NULL))
    {
        return pHead;
    }
    struct ListNode* newhead = ReverseList(pHead->next);
    pHead->next->next = pHead;
    pHead->next = NULL;
    return newhead;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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