目录
题目描述
解题代码
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode* head) { struct ListNode* newhead = NULL; struct ListNode* cur = head; struct ListNode* next = NULL; if(head == NULL) { return head; } else { while(cur) { if(newhead == NULL) { newhead =cur; next = cur->next; cur->next =NULL; cur = next; } else { next = cur->next; cur->next = newhead; newhead = cur; cur = next; } } } return newhead; }
解题思路
1、创建新头结点指针
2、对新头结点指针头插
3、记得将原头结点的next置空