1.0线性表之链表
程序员文章站
2022-06-06 13:32:57
...
基本概念
链表有单链表和双链表,二者区别如下:
示例演示
这里以领扣的206. 反转链表理解链表。
/**
* 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 head;
ListNode *cur = head->next, *p = NULL;
head->next = NULL;
while(cur){
p = cur->next;
cur->next = head;
head = cur;
cur = p;
}
return head;
}
};