-
Insert a node at a specific position in a linked list해커랭크(HackerRank) 2018. 8. 19. 17:18반응형
1. 문제
2. 알고리즘
키워드 - 구현, 자료구조, 링크리스트
3. 코드
123456789101112131415161718192021222324252627282930313233343536/*Insert Node at a given position in a linked listhead can be NULLFirst element in the linked list is at position 0Node is defined asstruct Node{int data;struct Node *next;}*/Node* InsertNth(Node *head, int data, int position){Node *newNode = new Node();//(Node*)malloc(sizeof(Node));newNode->data = data;if (head == NULL) {return newNode;}if (position == 0) {newNode->next = head;return newNode;}Node *currentNode = head;while (position - 1 > 0) {currentNode = currentNode->next;position--;}newNode->next = currentNode->next;currentNode->next = newNode;return head;}cs 반응형'해커랭크(HackerRank)' 카테고리의 다른 글
Reverse a linked list (0) 2018.08.19 Compare two linked lists (0) 2018.08.19 Counting Valleys (0) 2018.08.19 CamelCase (0) 2018.08.19 Interview Preparation KitArraysArrays: Left Rotation (0) 2018.07.04