我必须将 LL 的最后 n 个元素附加到前面
I have to append last n element of LL to front
node* append_LinkedList(node* head,int n)
{
//write your code here
int count=0;
node *temp=head;
while(count<n-1)
{
temp=temp->next;
count++;
}
cout<<temp->data;
return head;
}
下一步我无法思考。获得第 n 个节点指针后下一步该怎么做。
绝对看起来像是一个家庭作业问题。
考虑以下术语: 考虑以下链表,您希望将节点 C 移到前面。
A -> B -> C -> D -> E
将当前head
指针存储在变量中。遍历链表,直到找到C
。将C
的上一个节点标记为null
。将head
存储在C
处并循环,直到到达列表的末尾,在本例中为E
。现在,将E
的next
指向最初存储的head
所以你的输出将是C -> D -> E -> A -> B
Move last n element to front of a given Linked List. I am not getting correct output
node* append_LinkedList(node* head,int n)
{
//write your code here
int size=0;
node *temp2=head;
while(temp2->next!=NULL)
{
temp2=temp2->next;
size++;
}
int count=0;
node *temp=head;
node *head3=temp;
while(count<size-n-1)
{
temp=temp->next;
count++;
}
head=temp->next;
temp->next=NULL;
temp2->next=head3;
return head;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 我们可以在队列前面以 O(1) 时间复杂度排队一个元素吗?
- 我必须将 LL 的最后 n 个元素附加到前面
- 删除前面元素并将新元素添加到最小堆 (std::p riority_queue)
- memmove会移动元素(就像前面的方式相同),还是一次抓住整个内存块
- 如何在二维矢量前面添加元素
- 将std::deque的现有元素推到前面
- 容器具有恒定的时间,用于访问任何元素,从前面弹出并向后推
- 分配动态数组时,是否删除了前面的元素
- 如何将元素添加到链接列表的前面