给定一个包含N 个节点和整数X的双向链表(DLL),任务是找到整数X在双向链表中的位置。如果没有找到这样的位置,则打印-1 。
例子:
Input: 15 <=> 16 <=> 8 <=> 7 <=> 13, X = 8
Output: 3
Explanation: X (= 8) is present at the 3rd node of the doubly linked list.
Therefore, the required output is 3
Input: 5 <=> 3 <=> 4 <=> 2 <=> 9, X = 0
Output: -1
Explanation: X (= 0) is not present in the doubly linked list.
Therefore, the required output is -1
处理方法:按照以下步骤解决问题:
- 初始化一个变量,比如pos ,以存储包含数据值X的节点在双向链表中的位置。
- 初始化一个指针,比如temp ,来存储双向链表的头节点。
- 遍历链表,对于每个节点,检查该节点的数据值是否等于X。如果发现是真的,则打印pos 。
- 否则,打印-1 。
下面是上述方法的实现
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Structure of a node of
// the doubly linked list
struct Node {
// Stores data value
// of a node
int data;
// Stores pointer
// to next node
Node* next;
// Stores pointer
// to previous node
Node* prev;
};
// Function to insert a node at the
// beginning of the Doubly Linked List
void push(Node** head_ref, int new_data)
{
// Allocate memory for new node
Node* new_node
= (Node*)malloc(sizeof(struct Node));
// Insert the data
new_node->data = new_data;
// Since node is added at the
// beginning, prev is always NULL
new_node->prev = NULL;
// Link the old list to the new node
new_node->next = (*head_ref);
// If pointer to head is not NULL
if ((*head_ref) != NULL) {
// Change the prev of head
// node to new node
(*head_ref)->prev = new_node;
}
// Move the head to point to the new node
(*head_ref) = new_node;
}
// Function to find the position of
// an integer in doubly linked list
int search(Node** head_ref, int x)
{
// Stores head Node
Node* temp = *head_ref;
// Stores position of the integer
// in the doubly linked list
int pos = 0;
// Traverse the doubly linked list
while (temp->data != x
&& temp->next != NULL) {
// Update pos
pos++;
// Update temp
temp = temp->next;
}
// If the integer not present
// in the doubly linked list
if (temp->data != x)
return -1;
// If the integer present in
// the doubly linked list
return (pos + 1);
}
// Driver Code
int main()
{
Node* head = NULL;
int X = 8;
// Create the doubly linked list
// 18 <-> 15 <-> 8 <-> 9 <-> 14
push(&head, 14);
push(&head, 9);
push(&head, 8);
push(&head, 15);
push(&head, 18);
cout << search(&head, X);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG
{
// Structure of a node of
// the doubly linked list
static class Node
{
// Stores data value
// of a node
int data;
// Stores pointer
// to next node
Node next;
// Stores pointer
// to previous node
Node prev;
};
// Function to insert a node at the
// beginning of the Doubly Linked List
static Node push(Node head_ref, int new_data)
{
// Allocate memory for new node
Node new_node = new Node();
// Insert the data
new_node.data = new_data;
// Since node is added at the
// beginning, prev is always null
new_node.prev = null;
// Link the old list to the new node
new_node.next = head_ref;
// If pointer to head is not null
if (head_ref != null)
{
// Change the prev of head
// node to new node
head_ref.prev = new_node;
}
// Move the head to point to the new node
head_ref = new_node;
return head_ref;
}
// Function to find the position of
// an integer in doubly linked list
static int search(Node head_ref, int x)
{
// Stores head Node
Node temp = head_ref;
// Stores position of the integer
// in the doubly linked list
int pos = 0;
// Traverse the doubly linked list
while (temp.data != x
&& temp.next != null)
{
// Update pos
pos++;
// Update temp
temp = temp.next;
}
// If the integer not present
// in the doubly linked list
if (temp.data != x)
return -1;
// If the integer present in
// the doubly linked list
return (pos + 1);
}
// Driver Code
public static void main(String[] args)
{
Node head = null;
int X = 8;
// Create the doubly linked list
// 18 <-> 15 <-> 8 <-> 9 <-> 14
head = push(head, 14);
head = push(head, 9);
head = push(head, 8);
head = push(head, 15);
head = push(head, 18);
System.out.print(search(head, X));
}
}
// This code is contributed by Rajput-Ji
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Structure of a node of
// the doubly linked list
public class Node
{
// Stores data value
// of a node
public int data;
// Stores pointer
// to next node
public Node next;
// Stores pointer
// to previous node
public Node prev;
};
// Function to insert a node at the
// beginning of the Doubly Linked List
static Node push(Node head_ref, int new_data)
{
// Allocate memory for new node
Node new_node = new Node();
// Insert the data
new_node.data = new_data;
// Since node is added at the
// beginning, prev is always null
new_node.prev = null;
// Link the old list to the new node
new_node.next = head_ref;
// If pointer to head is not null
if (head_ref != null)
{
// Change the prev of head
// node to new node
head_ref.prev = new_node;
}
// Move the head to point to the new node
head_ref = new_node;
return head_ref;
}
// Function to find the position of
// an integer in doubly linked list
static int search(Node head_ref, int x)
{
// Stores head Node
Node temp = head_ref;
// Stores position of the integer
// in the doubly linked list
int pos = 0;
// Traverse the doubly linked list
while (temp.data != x &&
temp.next != null)
{
// Update pos
pos++;
// Update temp
temp = temp.next;
}
// If the integer not present
// in the doubly linked list
if (temp.data != x)
return -1;
// If the integer present in
// the doubly linked list
return (pos + 1);
}
// Driver Code
public static void Main(String[] args)
{
Node head = null;
int X = 8;
// Create the doubly linked list
// 18 <-> 15 <-> 8 <-> 9 <-> 14
head = push(head, 14);
head = push(head, 9);
head = push(head, 8);
head = push(head, 15);
head = push(head, 18);
Console.Write(search(head, X));
}
}
// This code is contributed by gauravrajput1
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live