From : https://leetcode.com/problems/reverse-linked-list/
Reverse a singly linked list.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *cur=head,*res = NULL;
while(cur) {
ListNode* nxt = cur->next;
cur->next = res;
res = cur;
cur = nxt;
}
return res;
}
};
本文详细介绍了如何使用迭代方法来实现链表的反转,并提供了一个简洁的C++代码示例。通过对链表节点的next指针进行操作,实现了链表元素顺序的翻转。
549

被折叠的 条评论
为什么被折叠?



