1.解题思路Ⅰ
利用头插法,遍历数组将后面的元素头插到前面的元素.
2.代码实现Ⅰ
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode*cur=head;;
struct ListNode*newhead=NULL;
while(cur)
{
struct ListNode*next=cur->next;
cur->next=newhead;
newhead=cur;
cur=next;
}
return newhead;
}
3.解题思路Ⅱ
利用三指针法,利用三个指针将原先的指向顺序改变
4.代码实现Ⅱ
struct ListNode* reverseList(struct ListNode* head)
{
if (head == NULL)
return NULL;
struct ListNode* p = head;
struct ListNode* plist = head;
struct ListNode* ps = head->next;
plist->next = NULL;
while (p != NULL)
{
p = ps;
if (ps != NULL) {
ps = ps->next;
p->next = plist;
plist = p;
}
}
return plist;
}
结尾:今天的分享到此结束,喜欢的朋友如果感觉有帮助可以点赞三连支持,咱们共同进步!