链表——翻转链表
struct ListNode
{
int val;
ListNode* next;
ListNode(int val) :val(val), next(NULL) {};
};
class Solution
{
public:
ListNode* reverseList(ListNode* head)
{
ListNode* temp; // 保存cur的下一个节点
ListNode* cur = head; // 初始化操作
ListNode* pre = NULL;
while (cur)
{
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};

浙公网安备 33010602011771号