题解 | #牛尾反转#
牛尾反转
https://www.nowcoder.com/practice/584881d8f6874fdeb49f987a77b54857
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* sortCows(ListNode* head) {
// write code here
ListNode vHead(-1);
while(head != nullptr) {
auto tmp = head->next;
AddFront(&vHead, head);
head = tmp;
}
return vHead.next;
}
// head -> t, head -> x -> t,
void AddFront(ListNode *head, ListNode *x) {
x->next = head->next;
head->next = x;
}
};
头插法
拼多多集团-PDD成长空间 1356人发布
