用C/C++编程写翻转链表
时间: 2023-04-06 13:01:52 浏览: 122
可以使用以下代码来用 C/C++ 编程写翻转链表:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* reverseList(ListNode* head) {
ListNode* prev = NULL;
ListNode* curr = head;
while (curr != NULL) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
阅读全文