我正在尝试编写一个函数,用于以这种方式旋转链表:1 -> 2 -> 3 -> 4,如果用户选择第一个节点向前移动 3 个节点,它将是:2->3->1->4->END(在两个节点之间切换,直到到达第三个节点,在我看来就像冒泡排序)。另一个例子:如果用户选择第二个节点作为第一个节点,它将如下所示:2->1->3->4->END。
最佳答案
大概像下面的流程。 (未经测试的伪代码)
void rotateLeftOne(Node *np, int rotateLength){
if(np == NULL || np->next == NULL)
return;
assert(rotateLength <= listLength(np));
for(int i = 1; i < rotateLength && np->next; ++i, np = np->next){
swap(&np->value, &np->next->value);
}
}
关于c - 使用冒泡排序旋转链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44539922/