이 문제에서는 이중 연결 목록 LL이 제공됩니다. 우리의 임무는 이중 연결 목록에서 가장 큰 노드를 찾는 것입니다. .
문제를 이해하기 위해 예를 들어 보겠습니다.
Input : linked-list = 5 -> 2 -> 9 -> 8 -> 1 -> 3 Output : 9
해결 방법
문제를 해결하는 간단한 방법은 연결 목록을 탐색하고 max의 데이터 값이 maxVal의 데이터보다 큰 경우입니다. 연결 목록을 탐색한 후 macVal 노드 데이터를 반환합니다.
예
솔루션 작동을 설명하는 프로그램
#include <iostream>
using namespace std;
struct Node{
int data;
struct Node* next;
struct Node* prev;
};
void push(struct Node** head_ref, int new_data){
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int findLargestNodeInDLL(struct Node** head_ref){
struct Node *maxVal, *curr;
maxVal = curr = *head_ref;
while (curr != NULL){
if (curr->data > maxVal->data)
maxVal = curr;
curr = curr->next;
}
return maxVal->data;
}
int main(){
struct Node* head = NULL;
push(&head, 5);
push(&head, 2);
push(&head, 9);
push(&head, 1);
push(&head, 3);
cout<<"The largest node in doubly linked-list is "<<findLargestNodeInDLL(&head);
return 0;
} 출력
The largest node in doubly linked-list is 9