定义链表结构体
typedef struct ListNode
{
int val;
struct ListNode *next;
}ListNode;
遍历链表
void print(ListNode *head)
{
for(ListNode *cursor = head; cursor; cursor = cursor->next)
{
printf("%d->",cursor->val);
}
}
尾部插入
void lpush_back(ListNode *head, int val)
{
ListNode *node = (ListNode*)malloc(sizeof(ListNode));
ListNode *cursor = head;
node->val = val;
while(cursor->next) cursor = cursor->next;
cursor->next = node;
}
头部插入
void lpush_front(ListNode **head, int val)
{
ListNode *node = (ListNode*)malloc(sizeof(ListNode));
node->val = val;
node->next =*head;
*head = node;
}
头部删除
删除指定节点