题意:将链表到序。
思路:简单模拟。
/**
* 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) {
if(!head) return NULL;
ListNode *next = head;
ListNode *pre = NULL;
ListNode *now = head;
while(next) {
head = next;
next = next->next;
now->next = pre;
pre = now;
now = next;
}
return head;
}
};

本文介绍了一种简单的链表逆序算法实现方法。通过迭代的方式改变链表节点的指向,最终达到逆序的效果。该算法适用于单链表的数据结构。
554

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



