Reverse Linked List

///  实现链表反转的两种方法


///  1. 创建新链表


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

}


/// 2 . 移动cur / head指针指向

struct ListNode* reverseList(struct ListNode* head) {
    if(head == NULL) return NULL;
    struct ListNode* pre = head;
    struct ListNode* cur = head->next;
    while(cur!=null){
        pre->next = cur->next;
        cur->next = head;
        head = cur;
        cur = pre->next;
    }
    return head;

}

发布了91 篇原创文章 · 获赞 6 · 访问量 5万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章