We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Get Node Value
Get Node Value
+ 0 comments int getNode(SinglyLinkedListNode* llist, int positionFromTail) { if (llist == NULL || positionFromTail < 0) { return -1; } SinglyLinkedListNode* temp = llist; int length = 0; while (temp != NULL) { length++; temp = temp->next; } if (positionFromTail >= length) { return -1; } temp = llist; for (int i = 0; i < length - positionFromTail - 1; i++) { temp = temp->next; } return temp->data; }
+ 0 comments def getNode(llist, positionFromTail): length=0 dummy=llist while dummy: dummy=dummy.next length+=1 for i in range(length-positionFromTail-1): llist=llist.next return llist.data
+ 0 comments int length = 0; SinglyLinkedListNode p = llist; while(llist !=null){ length++; llist=llist.next; } int position =0; while(position != length-positionFromTail-1){ p=p.next; position++; } return p.data;
+ 0 comments def getNode(llist, positionFromTail): pointer_node = llist while llist.next is not None: llist = llist.next if positionFromTail == 0: pointer_node = pointer_node.next else: positionFromTail -= 1 return pointer_node.data
+ 0 comments #include <string> #include <cstring> #include <iostream> #include <iomanip> #include <vector> #include <algorithm> #include <sstream> #include <map> #include <set> #include <cmath> #include <fstream> using namespace std; using ll = long long; struct node { int data; node* next; }; node* makeNode(int x) { node* newNode = new node; newNode->data = x; newNode->next = NULL; return newNode; } void addLast(node** head, int x) { node* newNode = makeNode(x); if (*head == NULL) { *head = newNode; return; } node* tmp = *head; while (tmp->next != NULL) tmp = tmp->next; tmp->next = newNode; } void inthongtin(node* head) { while (head != NULL) { cout << head->data << " "; head = head->next; } } void getValue(node* head, int vitri, int n) { int demvitri = 0; for (node* k = head; k != NULL; k = k->next) { demvitri++; if (demvitri + vitri == n) { cout << k->data << endl; break; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); node* head = NULL; int t; cin>>t; for(int j=0;j<t;j++) { int n; cin >> n; for (int i = 0; i < n; i++) { int y; cin >> y; addLast(&head, y); } int vitri;cin>>vitri; getValue(head,vitri, n); // free memory while (head != NULL) { node* tmp = head; head = head->next; delete tmp; } } }
Load more conversations
Sort 933 Discussions, By:
Please Login in order to post a comment