题目来源文章来源地址https://www.toymoban.com/news/detail-620273.html
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* current = head;
struct ListNode* newnode = NULL;
while(current!=NULL)
{
struct ListNode* next = current->next;
current->next = newnode;
newnode = current;
current = next;
}
return newnode;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head){
//反转链表使用头插法
//需要使用三个指针
struct ListNode* begin = head;
struct ListNode* end = NULL;
if(begin!=NULL)
end = begin->next;
struct ListNode* newhead = NULL;
while(begin!=NULL){
begin->next = newhead;
newhead = begin;
begin = end;
if(end!=NULL)
end = end->next;
}
return newhead;
}
文章来源:https://www.toymoban.com/news/detail-620273.html
到了这里,关于【单链表OJ题:反转链表】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!