点击打开链接
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return NULL;
if(head->next==NULL) return head;
ListNode *pre= head;
ListNode *cur= pre->next;
ListNode *Next;
int flag = 0;
while (cur) {
Next = cur->next;
if(flag==0){
pre->next = NULL; //设置第一个节点后置为NULL; 免得循环链表
flag = 1;
}
cur->next = pre; //重新设置连接
pre = cur; //移动节点
cur = Next;
}
return pre;
}
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return NULL;
if(head->next==NULL) return head;
ListNode *pre= NULL;
ListNode *cur= head;
ListNode *Next= NULL;
while (cur) {
Next = cur->next;
cur->next = pre; //重新设置连接
pre = cur; //移动节点
cur = Next;
}
return pre;
}
};