n个节点的二叉树n+1

Given a linked list and an integer n, append the last n elements of the LL to front. Assume given n will be smaller than length of LL.

给定一个链表和一个整数n,将LL的最后n个元素附加到前面。 假设给定的n将小于LL的长度。

Input format: Line 1: Linked list elements (separated by space and terminated by -1

输入格式:第1行:链接的列表元素(以空格分隔并以-1终止

    Sample Input 1 :
1 2 3 4 5 -1
3
Sample Output 1 :
3 4 5 1 2

Description:

描述:

The question asks us to append the last N nodes to front, i.e the new linked list should first start from those N nodes and then traverse the rest of the nodes through the head of the old linked list.

这个问题要求我们将最后的N个节点附加到前面,即新的链表应首先从这N个节点开始,然后再通过旧链表的头遍历其余节点。

Example:

例:

    For Linked List 1->2->3->4->5->6->NULL
To append the last 2 nodes, the new linked list should be:
5->6->1->2->3->4->NULL

Solution Explanation:

解决方案说明:

To solve this problem, we take two pointers temp and t and point both of them to the head of the linked list. We take another variable i and equate it to – n. This i is used for finding out the head of the new linked list. Then we traverse the loop while temp != NULL. In the loop we check that if(i>=0) i.e temp is now n nodes away from t, t = t-> next. We will update i++ and temp = temp->next on each traversal. At last, we update temp-> next = head, head = t -> next and t-> next = NULL.

为了解决这个问题,我们使用两个指针temp和t并将它们都指向链接列表的开头。 我们采用另一个变量i并将其等于– n 。 我用于查找新链表的标题。 然后,我们在temp!= NULL时遍历循环。 在循环中,我们检查if(i> = 0),即temp现在距离t距离n个节点, t = t-> next 。 我们将在每次遍历时更新i ++和temp = temp-> next 。 最后,我们更新temp-> next = head , head = t-> next和t-> next = NULL 。

Algorithm:

算法:

  • STEP 1: Declare the function appendNNode with parameters (Node* head, int n)

    步骤1:使用参数声明函数appendNNode (Node * head,int n)

  • STEP 2: Declare two variables Node * temp , t and point both of them to head.

    步骤2:声明两个变量Node * temp , t并将它们都指向head。

  • STEP 3: Declare int i = -n

    步骤3:声明int i = -n

  • STEP 4: Repeat Step 5 and 6, while(temp->next != NULL)

    步骤4:重复步骤5和6, 同时(temp-> next!= NULL)

  • STEP 5: if(i>=0) t = t-> next.

    步骤5: if(i> = 0)t = t-> next 。

  • STEP 6: temp = temp-> next, i++.

    步骤6: temp = temp->接下来,i ++ 。

  • STEP 7: temp->next = head, head = t->next, and t-> next =NULL

    步骤7: temp-> next = head , head = t-> next和t-> next = NULL

  • STEP 8: return head

    步骤8:返回头

Steps:

脚步:

    At first: 1->2->3->4->5->6->NULL, t->1 and temp->1.
After complete traversal: 1->2->3->4->5->6->NULL, t->4 and temp->6.
So, temp->next = head and head = t->next
i.e 5->6->1->2->3->4 --- (reconnecting to 5)
Atlast, t-> next = NULL
i.e 5->6->1->2->3->4->NULL
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Function:

功能:

Node *appendNNodes(Node* head, int n){
// Two pointers, one for traversal and
// other for finding the new head of LL
Node *temp = head, *t = head;
//index maintained for finding new head
int i = -n;
while(temp->next!=NULL){
//When temp went forward n nodes from t
if(i>=0){
t = t->next;
}
temp = temp ->next;
i++;
}
//Connecting the tail to head
temp->next = head;
//Assigning the new node
head = t->next;
//Deleting the previous connection
t->next = NULL;
return head;
}

C++ Code:

C ++代码:

#include<bits/stdc++.h>
using namespace std;
struct Node{// linked list Node
int data;
Node * next;
};
Node *newNode(int k){ //defining new node
Node *temp = (Node*)malloc(sizeof(Node));
temp->data = k;
temp->next = NULL;
return temp;
}
//Used to add new node at the end of the list
Node *addNode(Node* head, int k){if(head == NULL){head = newNode(k);
}
else{Node * temp = head;
Node * node = newNode(k);
while(temp->next!= NULL){temp = temp->next;
}
temp-> next = node;
}
return head;
}
// Used to create new linked list and return head
Node *createNewLL(){int cont = 1;
int data;
Node* head = NULL;
while(cont){cout<<"Enter the data of the Node"<<endl;
cin>>data;
head = addNode(head,data);
cout<<"Do you want to continue?(0/1)"<<endl;
cin>>cont;
}
return head;
}
//To print the Linked List
void *printLL(Node * head){while(head!= NULL){cout<<head->data<<"->";
head = head-> next;
}
cout<<"NULL"<<endl;
}
//Function
Node *appendNNodes(Node* head, int n){// Two pointers, one for traversal and
// other for finding the new head of LL
Node *temp = head, *t = head;
//index maintained for finding new head
int i = -n;
while(temp->next!=NULL){//When temp went forward n nodes from t
if(i>=0){
t = t->next;
}
temp = temp ->next;
i++;
}
//Connecting the tail to head
temp->next = head;
//Assigning the new node
head = t->next;
//Deleting the previous connection
t->next = NULL;
return head;
}
//Driver Main
int main(){Node * head = createNewLL();
cout<<"The linked list is"<<endl;
printLL(head);
int data;
cout<<"Enter the number of nodes you want to append."<<endl;
cin>>data;
head = appendNNodes(head,data);
cout<<"The new Linked List is" <<endl;
printLL(head);
return 0;
}
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

Output

输出量

Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
3
Do you want to continue?(0/1)
1
Enter the data of the Node
4
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
6
Do you want to continue?(0/1)
1
Enter the data of the Node
7
Do you want to continue?(0/1)
0
The linked list is
1->2->3->4->5->6->7->NULL
Enter the number of nodes you want to append.
3
The new Linked List is
5->6->7->1->2->3->4->NULL

翻译自: https://www.includehelp.com/cpp-programs/append-last-n-nodes-to-first-in-the-linked-list.aspx

n个节点的二叉树n+1

n个节点的二叉树n+1_使用C ++程序将链接列表中的最后N个节点附加到第一个相关推荐

  1. n个节点的二叉树n+1_使用C ++程序删除链接列表的M个节点后的N个节点

    n个节点的二叉树n+1 Problem statement: 问题陈述: Given a Linked List, we have to delete N numbers of nodes after ...

  2. 微信小程序 查找兄弟节点_使用C ++程序在链接列表中查找节点

    微信小程序 查找兄弟节点 Given a linked list and an integer N, you need to find and return index where N is pres ...

  3. 二叉树打印叶子节点,非递归_使用递归打印链接列表中的备用节点

    二叉树打印叶子节点,非递归 Solution: 解: Input: A singly linked list whose address of the first node is stored in ...

  4. python链表删除尾部节点_python单链表中如何查找和删除节点?

    在之前的文章[python单链表中如何插入和输出节点?]中给大家介绍了单链表是什么,以及如何进行添加节点.输出所以节点.下面本篇文章给大家介绍如何查找和删除节点,希望对大家有所帮助. 如何从单链表中查 ...

  5. notepad++节点_在C ++中删除链接列表的中间节点

    notepad++节点 Given a single Linked List and we have to delete the middle the element of the Linked Li ...

  6. 【LeetCode】剑指 Offer 22. 链表中倒数第k个节点

    [LeetCode]剑指 Offer 22. 链表中倒数第k个节点 文章目录 [LeetCode]剑指 Offer 22. 链表中倒数第k个节点 一.遍历 二.双指针 总结 一.遍历 先遍历统计链表长 ...

  7. c++语言偶数分离,在C++的链表中分离偶数和奇数节点

    所以,我自学数据结构和算法.在解决一些问题时,我遇到了下面的问题,我必须分离链表的奇偶节点.在C++的链表中分离偶数和奇数节点 以下是问题陈述: 鉴于整数链表,编写一个函数来修改链表,使所有偶数在修改 ...

  8. 【ABAQUS笔记】ABAQUS中如何提取变形后节点的空间坐标?后附在matlab中处理.inp文件并绘制节点的程序

    1.新建一个ABAQUS工作界面,点选顶部菜单栏File-Import-Part. 2.弹出对话框下面的文件类型选".odb",选中需要提取节点坐标的结果文件. 3.弹出的对话框中 ...

  9. Hdfs NameNode中数据块、数据节点及副本状态概述

    HDFS集群以Master-Slave模式运行,主要有两类节点:一个Namenode(即Master)和多个Datanode(即Slave). ​ 在hdfs文件系统中,NameNode是HDFS中的 ...

最新文章

  1. java实时获取android网速_获取Android网速的另一种方法
  2. 图像的评价指标之SSMI——结构相似性
  3. JS中this关键字
  4. 杂项:TMT(数字媒体产业)
  5. mac下常用数据库及nginx笔记
  6. 神奇的go语言(image网站开发)
  7. POJ3281:Dining——题解
  8. “第八届吴文俊人工智能科学技术奖”揭晓,AI大佬们都说了什么?
  9. 360浏览器导出Excel闪退BUG
  10. UEFI shell - 标准应用程序的编译和加载过程
  11. NCI和CNC应用中M代码的概念和功能
  12. 如何在PPT中制作动态图表,学会这种方法实在太简单
  13. Python之函数的返回值
  14. 马化腾:这个市场不是拼钱、拼流量,而是拼团队、拼使命感和危机感
  15. 《私募股权基金投资基础知识》---第一章
  16. 'Bullet' object has no attribute 'draw_bullet'
  17. 智慧协同新应用:FEv6.6,让组织更灵动/敏捷!
  18. scipy.stats 用法
  19. COMSOL仿真进阶RF及波动光学模块
  20. Winform控件开发(15)——contextMenuStrip(史上最全)

热门文章

  1. 阿里云mysql创建多个用户_阿里云MySQL创建指定用户访问指定表
  2. daocloud创建mysql_GitHub - DaoCloud/php-apache-mysql-sample
  3. linux blender骨骼绑定,在Linux系统中安装开源3D创建套件Blender的方法
  4. antd react dva在model中使用另一个model的state值
  5. Jquery——hover与toggle
  6. CSS position(定位)属性
  7. Appium环境搭建-完整版
  8. MySQL(介绍,安装,密码操作,权限表)
  9. 查找出系统中大于50k 且小于100k 的文件并删除。
  10. php 修改 wordpress,wordpress怎么编辑代码修改页面