206. 反转链表 - 力扣(LeetCode)
思路:创建三个指针n1,n2,n3,遍历原链表,通过三者之间的关系将链表反转。下面给出图示:
下面给出题解代码:
typedef struct ListNode ListNode;
struct ListNode* reverseList(struct ListNode* head)
{//如果原链表为空,直接返回NULLif(head == NULL){return NULL;}//原链表不为空ListNode*n1 = NULL;ListNode*n2 = head;ListNode*n3 = head->next;while(n2){n2->next = n1;n1 = n2;n2 = n3;if(n3!=NULL){n3 = n3->next;}}return n1;
}
完!