📌  相关文章
📜  删除两个单向链表中的公共节点

📅  最后修改于: 2021-09-03 13:52:10             🧑  作者: Mango

给定两个链表L1L2 ,任务是从给定的两个链表中生成一个没有公共元素的新链表。

例子:

方法:

  1. 对于第一个链表中的每个元素(比如X ):
    • 遍历第二个链表并检查X是否存在于链表中。
    • 如果X不存在,则在结果链表中插入X ,因为它在两个链表中都不常见。
  2. 对于第二个链表中的每个元素(比如Y ):
    • 遍历第一个链表并检查Y是否存在于链表中。
    • 如果Y不存在,则在结果链表中插入Y ,因为它在两个链表中都不常见。
  3. 结果链表将是所需的链表,其中没有给定的两个链表的公共节点。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Link list node
struct Node {
    int data;
    struct Node* next;
};
 
// Function to print the element
// of the Linked List
void printList(struct Node* p)
{
    if (p == NULL) {
        cout << "[]";
    }
 
    while (p != NULL) {
        cout << p->data << " -> ";
        p = p->next;
    }
}
 
// Function to push the node at the
// beginning of the linked list
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->next = (*head_ref);
    (*head_ref) = new_node;
}
 
// Function to insert unique
// elements in the new LL
void traverse(struct Node** head3,
              struct Node* temp1,
              struct Node* temp2)
{
 
    // Traverse the first linked list
    while (temp1 != NULL) {
 
        // Value of current node
        int val = temp1->data;
        struct Node* t = temp2;
        int x = 0;
 
        // Traverse the second list
        while (t != NULL) {
            if (t->data == val) {
                x = 1;
                break;
            }
            t = t->next;
        }
 
        // If element is not common
        // then insert it in the
        // resultant linked list
        if (x == 0) {
            push(head3, temp1->data);
        }
 
        temp1 = temp1->next;
    }
}
 
// Function to remove the common nodes
// in two Singly Linked Lists
void removeCommonNodes(struct Node* head1,
                       struct Node* head2)
{
 
    // Head pointer for the resultant
    // linked list
    struct Node* head3 = NULL;
 
    // Find the node common between
    // linked list taking head1 as
    // first linked list
    traverse(&head3, head1, head2);
 
    // Find the node common between
    // linked list taking head2 as
    // first linked list
    traverse(&head3, head2, head1);
 
    // Print the resultant linked list
    printList(head3);
}
 
// Driver code
int main()
{
    // First list
    struct Node* head1 = NULL;
    push(&head1, 20);
    push(&head1, 5);
    push(&head1, 15);
    push(&head1, 10);
 
    // Second list
    struct Node* head2 = NULL;
    push(&head2, 10);
    push(&head2, 20);
    push(&head2, 15);
    push(&head2, 8);
 
    // Function call
    removeCommonNodes(head1, head2);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
// Link list node
static class Node {
    int data;
    Node next;
};
  
// Function to print the element
// of the Linked List
static void printList(Node p)
{
    if (p == null) {
        System.out.print("[]");
    }
  
    while (p != null) {
        System.out.print(p.data+ "->");
        p = p.next;
    }
}
  
// Function to push the node at the
// beginning of the linked list
static Node push(Node head_ref,
          int new_data)
{
    Node new_node = new Node();
  
    new_node.data = new_data;
    new_node.next = head_ref;
    head_ref = new_node;
    return head_ref;
}
  
// Function to insert unique
// elements in the new LL
static Node traverse(Node head3,
              Node temp1,
              Node temp2)
{
  
    // Traverse the first linked list
    while (temp1 != null) {
  
        // Value of current node
        int val = temp1.data;
        Node t = temp2;
        int x = 0;
  
        // Traverse the second list
        while (t != null) {
            if (t.data == val) {
                x = 1;
                break;
            }
            t = t.next;
        }
  
        // If element is not common
        // then insert it in the
        // resultant linked list
        if (x == 0) {
            head3 = push(head3, temp1.data);
        }
  
        temp1 = temp1.next;
    }
    return head3;
}
  
// Function to remove the common nodes
// in two Singly Linked Lists
static void removeCommonNodes(Node head1,
                       Node head2)
{
  
    // Head pointer for the resultant
    // linked list
    Node head3 = null;
  
    // Find the node common between
    // linked list taking head1 as
    // first linked list
    head3 = traverse(head3, head1, head2);
  
    // Find the node common between
    // linked list taking head2 as
    // first linked list
    head3 = traverse(head3, head2, head1);
  
    // Print the resultant linked list
    printList(head3);
}
  
// Driver code
public static void main(String[] args)
{
    // First list
    Node head1 = new Node();
    head1 = push(head1, 20);
    head1 = push(head1, 5);
    head1 = push(head1, 15);
    head1 = push(head1, 10);
  
    // Second list
    Node head2 = new Node();
    head2 = push(head2, 10);
    head2 = push(head2, 20);
    head2 = push(head2, 15);
    head2 = push(head2, 8);
  
    // Function call
    removeCommonNodes(head1, head2);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program for the above approach
  
# Link list node
class Node:
     
    def __init__(self):
         
        self.data = 0
        self.next = None
     
# Function to print the element
# of the Linked List
def printList(p):
     
    if (p == None):
        print('[]', end = '')
  
    while (p != None):
        print(p.data, end = ' -> ')
        p = p.next
     
# Function to push the node at the
# beginning of the linked list
def push(head_ref, new_data):
     
    new_node = Node()
    new_node.data = new_data
    new_node.next = (head_ref)
    (head_ref) = new_node
     
    return head_ref
 
# Function to insert unique
# elements in the new LL
def traverse(head3, temp1, temp2):
     
    # Traverse the first linked list
    while (temp1 != None):
         
        # Value of current node
        val = temp1.data
        t = temp2
        x = 0
  
        # Traverse the second list
        while (t != None):
            if (t.data == val):
                x = 1
                break
         
            t = t.next
  
        # If element is not common
        # then insert it in the
        # resultant linked list
        if (x == 0):
            head3 = push(head3, temp1.data)
  
        temp1 = temp1.next
         
    return head3
  
# Function to remove the common nodes
# in two Singly Linked Lists
def removeCommonNodes(head1, head2):
     
    # Head pointer for the resultant
    # linked list
    head3 = None
  
    # Find the node common between
    # linked list taking head1 as
    # first linked list
    head3 = traverse(head3, head1, head2)
  
    # Find the node common between
    # linked list taking head2 as
    # first linked list
    head3 = traverse(head3, head2, head1)
  
    # Print the resultant linked list
    printList(head3)
 
# Driver code
if __name__=='__main__':
     
    # First list
    head1 = None
    head1 = push(head1, 20)
    head1 = push(head1, 5)
    head1 = push(head1, 15)
    head1 = push(head1, 10)
  
    # Second list
    head2 = None
    head2 = push(head2, 10)
    head2 = push(head2, 20)
    head2 = push(head2, 15)
    head2 = push(head2, 8)
  
    # Function call
    removeCommonNodes(head1, head2)
 
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
 
class GFG{
   
// Link list node
class Node {
    public int data;
    public Node next;
};
   
// Function to print the element
// of the Linked List
static void printList(Node p)
{
    if (p == null) {
        Console.Write("[]");
    }
   
    while (p != null) {
        Console.Write(p.data+ "->");
        p = p.next;
    }
}
   
// Function to push the node at the
// beginning of the linked list
static Node push(Node head_ref,
          int new_data)
{
    Node new_node = new Node();
   
    new_node.data = new_data;
    new_node.next = head_ref;
    head_ref = new_node;
    return head_ref;
}
   
// Function to insert unique
// elements in the new LL
static Node traverse(Node head3,
              Node temp1,
              Node temp2)
{
   
    // Traverse the first linked list
    while (temp1 != null) {
   
        // Value of current node
        int val = temp1.data;
        Node t = temp2;
        int x = 0;
   
        // Traverse the second list
        while (t != null) {
            if (t.data == val) {
                x = 1;
                break;
            }
            t = t.next;
        }
   
        // If element is not common
        // then insert it in the
        // resultant linked list
        if (x == 0) {
            head3 = push(head3, temp1.data);
        }
   
        temp1 = temp1.next;
    }
    return head3;
}
   
// Function to remove the common nodes
// in two Singly Linked Lists
static void removeCommonNodes(Node head1,
                       Node head2)
{
   
    // Head pointer for the resultant
    // linked list
    Node head3 = null;
   
    // Find the node common between
    // linked list taking head1 as
    // first linked list
    head3 = traverse(head3, head1, head2);
   
    // Find the node common between
    // linked list taking head2 as
    // first linked list
    head3 = traverse(head3, head2, head1);
   
    // Print the resultant linked list
    printList(head3);
}
   
// Driver code
public static void Main(String[] args)
{
    // First list
    Node head1 = new Node();
    head1 = push(head1, 20);
    head1 = push(head1, 5);
    head1 = push(head1, 15);
    head1 = push(head1, 10);
   
    // Second list
    Node head2 = new Node();
    head2 = push(head2, 10);
    head2 = push(head2, 20);
    head2 = push(head2, 15);
    head2 = push(head2, 8);
   
    // Function call
    removeCommonNodes(head1, head2);
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
8 -> 5 ->

时间复杂度: O(M * N),其中 M 和 N 是两个给定链表的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live