C语言 反转链表
题目链接:https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=selected-coding-interview
完整代码:
/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/// 反转链表!
//
struct ListNode* reverseList(struct ListNode* head) {// struct ListNode* pre = (struct ListNode*)malloc(sizeof(struct ListNode));// struct ListNode* cur = (struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* pre = NULL; // 如果是空节点, 那么直接写 NULL!struct ListNode* cur = head;while(cur) {// 这里为什么不申请内存。而是直接就了呢?struct ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;
}
问题:
// 这里为什么不申请内存。而是直接就使用呢?
struct ListNode* next = cur->next;
我的理解是: cur->next , 也是一个指针。 所以这里是2个指针之间的传递。
也许我的理解不对。
暂时先记录一下。