【C语言刷LeetCode】206. 反转链表(E)

反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

链表题经常需要申请一个虚拟节点,此题便是。

struct ListNode* reverseList(struct ListNode* head){
    struct ListNode* newp;
    struct ListNode* curp = head;
    struct ListNode* top = head;

    if (head == NULL) return NULL;
    newp = (ListNode*)malloc(sizeof(struct ListNode));

    while(head) { 
        newp->next = top;
        head = head->next;
        top->next = curp;
        curp = top;
        top = head;
    }

    head = newp->next;
    return head;
}
发布了124 篇原创文章 · 获赞 17 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/jin615567975/article/details/104382153
今日推荐