给定一个链表和两个整数X和Y ,任务是在值X的节点第一次出现后删除所有出现的Y并打印修改后的链表。
例子:
Input: 7 → 20 → 9 → 10 → 20 → 14 → 15 → 20, X = 10, Y = 20
Output: 7 → 20 → 9 → 10 → 14 → 15
Input: LL: 10 → 20, X = 10, Y = 20
Output: LL: 10
方法:可以通过遍历给定的链表并删除值X的节点之后出现值Y 的所有节点来解决给定的问题。请按照以下步骤解决问题:
- 初始化两个链表节点K和prev ,以存储给定链表的当前头部和链表当前头部的前一个节点。
- 遍历给定的链表直到K变为NULL并执行以下步骤:
- 迭代节点 K直到找到值为X的节点,同时将节点prev更新为前一个节点 K 。
- 将节点 K存储在另一个变量中,比如temp ,并遍历链表,直到出现值为Y的节点,同时将节点prev更新为前一个节点 K 。
- 如果temp 的值为NULL ,则跳出循环。否则,将prev的next 指针更新为temp的 next 指针,将temp更新为节点prev的 next 指针。
- 完成以上步骤后,打印修改后的链表。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
import java.util.LinkedList;
class Main {
// Head of the linked list
static Node head;
// Structure of a node
// of a Linked List
class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
// Function to delete all occurrences
// of key after first occurrence of A
void deleteKey(int A, int key)
{
// Stores the head node
Node k = head, prev = null;
while (k != null) {
// Iterate until the
// node A occurrs
while (k != null
&& k.data != A) {
prev = k;
k = k.next;
}
Node temp = k;
while (temp != null
&& temp.data != key) {
prev = temp;
temp = temp.next;
}
// If the entire linked
// list has been traversed
if (temp == null)
return;
// Update prev and temp node
prev.next = temp.next;
temp = prev.next;
}
}
// Function to insert a new Node
// at the front of the Linked List
public void push(int new_data)
{
// Create a new node
Node new_node = new Node(new_data);
// Insert the node at the front
new_node.next = head;
// Update the head of LL
head = new_node;
}
// Function to print the Linked List
public void printList()
{
// Stores the head node
Node tnode = head;
// Traverse the Linked List
while (tnode != null) {
// Print the node
System.out.print(tnode.data + " ");
// Update tnode
tnode = tnode.next;
}
}
// Driver Code
public static void main(String[] args)
{
Main list = new Main();
list.push(20);
list.push(15);
list.push(10);
list.push(20);
list.push(10);
list.push(9);
list.push(20);
list.push(7);
int X = 10;
int Y = 20;
list.deleteKey(X, Y);
// Print the updated list
list.printList();
}
}
Python3
# Python3 program for the above approach
# Structure of a node
# of a Linked List
class Node:
def __init__(self, x):
self.data = x
self.left = None
self.right = None
# Function to delete all occurrences
# of key after first occurrence of A
def deleteKey(head, A, key):
# Stores the head node
k, prev = head, None
while (k != None):
# Iterate until the
# node A occurrs
while (k != None and k.data != A):
prev = k
k = k.next
temp = k
while (temp != None and temp.data != key):
prev = temp
temp = temp.next
# If the entire linked
# list has been traversed
if (temp == None):
return
# Update prev and temp node
prev.next = temp.next
temp = prev.next
return head
# Function to insert a new Node
# at the front of the Linked List
def push(head, new_data):
# Create a new node
new_node = Node(new_data)
# Insert the node at the front
new_node.next = head
# Update the head of LL
head = new_node
return head
# Function to print the Linked List
def printList(head):
# Stores the head node
tnode = head
# Traverse the Linked List
while (tnode.next != None):
# Print the node
print(tnode.data, end = " ")
# Update tnode
tnode = tnode.next
# Driver Code
if __name__ == '__main__':
list = None
list = push(list, 20)
list = push(list, 15)
list = push(list, 10)
list = push(list, 20)
list = push(list, 10)
list = push(list, 9)
list = push(list, 20)
list = push(list, 7)
X = 10
Y = 20
list = deleteKey(list, X, Y)
# Print the updated list
printList(list)
# This code is contributed by mohit kumar 29
Javascript
输出:
7 20 9 10 10 15
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live