链表节点定义:
class ListNode///单链表节点 { public: int val; ListNode *next; ListNode(int x): val(x),next(nullptr) {} }; struct RandomListNode{///随机链表节点 int label; RandomListNode *next,*random; RandomListNode(int x):label(x),next(nullptr),random(nullptr){} }; class TreeNode {///树节点表示 public: int val; TreeNode *left; TreeNode *right; TreeNode(int x): val(x),left(nullptr),right(nullptr){} };
就地逆置
ListNode* revOnPlace(ListNode *head) { ListNode dummy(-1);//假的头节点 ListNode *curr = head; head = &dummy; while(curr) { ListNode *tmp = curr->next; curr->next = head->next; head->next = curr; curr = tmp; } return dummy.next; }