首页 > 代码库 > 翻转链表

翻转链表

之前做过链表的翻转,这里看到一个更简单的实现

ListNode* Reverse(ListNode *head) {
    ListNode *reHead = NULL;
    ListNode *prev = NULL;
    ListNode *Node = head;
    while(Node != NULL) {
        ListNode *next = Node->next;
        if(next == NULL)
          reHead = Node;
        Node->next = prev;
        prev = Node;
        Node = next;
    }
   return reHead;
 }

还有

    ListNode* reverse(ListNode *head){
        if(head == NULL || head->next == NULL )
            return head;
        ListNode* tmp = reverse(head->next);
        head->next->next = head;
        head->next = NULL;
        return tmp;
    }



翻转链表