leetcode 206 翻转链表

leetcode 206 翻转链表_第1张图片

首先我们做这种题目 只有画图是最容易看出来的,再写代码也不迟,比方说你一上来就刷刷刷写,写完之后一堆报错,调试调死你。

leetcode 206 翻转链表_第2张图片

代码奉上:

struct ListNode* reverseList(struct ListNode* head){

if(head==NULL)
{
    return head;
}
struct ListNode*n1,*n2,*n3;
n1=NULL;
n2=head;
n3=head->next;
while(n2)
{
    n2->next=n1;
    n1=n2;
    n2=n3;
    if(n3)//如果n3不为空才进入
    n3=n3->next;
}
return n1;

}

leetcode 206 翻转链表_第3张图片

 如有错误,或者更好的方法欢迎留言交流!

你可能感兴趣的:(leetcode,leetcode,链表,算法)