LeetCode 面试题24. 反转链表
题目链接:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
限制:
0 <= 节点个数 <= 5000
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 9 struct ListNode* reverseList(struct ListNode* head){ 10 if(head==NULL||head->next==NULL) return head; 11 struct ListNode *pre=NULL,*cur=head,*tmp; 12 while(cur){ 13 tmp=cur->next; 14 cur->next=pre; 15 pre=cur; 16 cur=tmp; 17 } 18 return pre; 19 }