leetcode之反转链表

来源:互联网 发布:杭州电商美工设计培训 编辑:程序博客网 时间:2024/06/16 13:06

原文链接:点击打开链接


Reverse a singly linked list

A linked list can be reversed either iteratively or recursively. Could you implement both?


struct ListNode* reverseList(struct ListNode* head) {// 链表为空或者只有头结点if (!head || !head->next)return head; struct ListNode* p1 = head, *p2 = head->next, *p3 = p2;while (NULL != p2) {p3 = p2->next;p2->next = p1;p1 = p2;p2 = p3;}head->next = NULL;return (head = p1);}



0 0