Small tricks in brushing questions

Small tricks in brushing questions

linked list

Delete linked list node

题目一:在O(1)时间内删除链表节点。
	给定一个单链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点。

Conventional O(n) idea: start traversing from the head pointer, find the previous node of the pointer to be deleted, and then point to the next node of the node to be deleted.

Trick idea O(1): Find the next node of the node to be deleted, then overwrite its value and other content on the node to be deleted, and then delete the next node.
Small problems: 1) The last node to be deleted is the last node, and there is no next node, so it is still necessary to traverse from the head pointer.

Guess you like

Origin blog.csdn.net/weixin_42213421/article/details/126086572