题目描述
输入一个链表,反转链表后,输出链表的所有元素。
代码实现
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* k = NULL;
ListNode* l = NULL;
while(pHead!=NULL){
k = pHead;
pHead = k->next;
k->next = l;
l = k;
}
return l ;
}
};
本文介绍了一种链表反转的方法,并提供了详细的代码实现。通过迭代方式,该算法能将输入的链表顺序反转并输出所有元素。
172万+

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



