Appearance
206. 反转链表
c
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head){
struct ListNode *mHead =
(struct ListNode *)malloc(sizeof(struct ListNode));
mHead->val = 0;
mHead->next = head;
struct ListNode *p = mHead->next;
mHead->next = NULL;
while (p != NULL)
{
struct ListNode *s = p;
p = p->next;
/* 头部插入 */
s->next = mHead->next;
mHead->next = s;
}
p = mHead->next;
free(mHead);
return p;
}因为课上教的链表有头结点,结果这个题目的测试用例里没有,懒得思考那么多,直接给它创建一个用。