【Hackerrank】Get the value of the node at a specific position from the tail
来源:互联网 发布:学唱韩文歌的软件 编辑:程序博客网 时间:2024/06/07 03:52
You’re given the pointer to the head node of a linked list and a specific position. Counting backwards from the tail node of the linked list, get the value of the node at the given position. A position of 0 corresponds to the tail, 1 corresponds to the node before the tail and so on.
Input Format
You have to complete the int GetNode(Node* head, int positionFromTail)
method which takes two arguments - the head of the linked list and the position of the node from the tail. positionFromTail will be at least 0 and less than the number of nodes in the list. You should NOT read any input from stdin/console.
Output Format
Find the node at the given position counting backwards from the tail. Then return
the data
contained in this node. Do NOT print anything to stdout/console.
Sample Input
1 -> 3 -> 5 -> 6 -> NULL, positionFromTail = 01 -> 3 -> 5 -> 6 -> NULL, positionFromTail = 2
Sample Output
63 c++ code :
#include <iostream>#include<cstdio>#include<cstdlib>using namespace std;struct Node{int data;Node *next;};/* Get Nth element from the end in a linked list of integers Number of elements in the list will always be greater than N. Node is defined as struct Node { int data; struct Node *next; }*/int GetNode(Node *head,int positionFromTail){ // This is a "method-only" submission. // You only need to complete this method. if(head == NULL) return 0; Node *first = head; Node *sec = head; for(int i = 0; i < positionFromTail; i++) sec = sec->next; while(sec->next != NULL) { sec = sec->next; first = first->next; } return first->data;}void Print(Node *head){bool ok = false;while(head != NULL){if(ok)cout<<" ";else ok = true;cout<<head->data;head = head->next;}}Node* Insert(Node *head,int x){ Node *temp = new Node(); temp->data = x; temp->next = NULL; if(head == NULL) { return temp; } Node *temp1; for(temp1 = head;temp1->next!=NULL;temp1= temp1->next); temp1->next = temp;return head;}int main(){int t;cin>>t;while(t-- >0){Node *A = NULL;int m;cin>>m;while(m--){int x; cin>>x;A = Insert(A,x);}int n;cin>>n;cout<<GetNode(A,n)<<"\n";}}
- 【Hackerrank】Get the value of the node at a specific position from the tail
- Compute the region of specific value inside a dataset
- ios::ate places the get-position pointer at the file end, enable tellg() to return the size of the f
- ASP.NET 2.0: How to get a specific ConnectionString from the Web.Config by name
- The tail of MongoDB
- THE VALUE OF A SMILE
- Please change the value of 'Security.salt' in app/config/core.php to a salt value specific to your a
- Identify in details the consumption of memory from a specific session
- get the return value from the new activity
- The color chooser class, you could slide the scroll bars to get the specific numbers of the color
- remove the nth node from the end of list leetcode
- Adding a device from a kernel module the udev way (pi, a device that prints out the value of pi)
- P20 (*) P21 (*)Remove the Kth element from a list;Insert an element at a given position into a list.
- the number of position
- How to get the data from a cell when I click on the GridButtonColumn of the same row
- Get the Degree of Angle Between Hour and Minute Hand of a Clock at Anytime
- Get the Mime Type from a File
- Get the Mime Type from a File
- 在Visual C++中使用内联汇编
- 浏览器缓存机制
- 让你彻底理解STP的各种角色选举
- QEMU 调试内核
- ActivityManager 讲解
- 【Hackerrank】Get the value of the node at a specific position from the tail
- 古朝鮮 (朝鮮歷史 韓長庚)
- 原始生活 (朝鮮歷史 韓長庚)
- java socke学习
- 如何减小与“大牛”的差距
- Product(大数乘法)
- oracle中与索引相关的视图---all_indexes
- 我國의 自然環境 (朝鮮歷史 韓長庚)
- 指针的应用,有相同数字?