链表操作函数集合(C++)

2020-12-08 10:10:25 浏览数 (1)

反转单向链表

代码语言:javascript复制
void ReverseList(Node* head) {
	//反转带头节点指针的单向链表
	Node* pre = NULL;
	Node* cur = head->next;
	Node* nex = cur->next;
	
	while (cur != NULL) {
		cur->next = pre;
		pre = cur;
		cur = nex;
		nex = (nex == NULL) ? NULL : nex->next; 	
	}
	head->next = pre;

}

0 人点赞