📜  在给定节点之前在链表中插入一个节点

📅  最后修改于: 2021-09-08 12:43:11             🧑  作者: Mango

给定链表N的节点和值K,任务是将值K的节点插入链表中给定节点 N之前。

节点结构:

C++
// Structure of Node
struct Node {
    int data;
    Node* next;
 
    // Constructor of Node
    Node(int val, Node* link = 0)
        : data(val), next(link)
    {
    }
};


Java
// Structure of Node
public class Node
{
    public int data;
    public Node next;
   
    // Constructor of Node
    public Node(int val, Node link = null)
    {
        this.data = val;
        this.next = link;
    }
}
 
// This code is contributed by divyesh072019


Python3
# Structure of Node
class Node:
     
    # Constructor of Node
    def __init__(self, val, link = None):
         
        self.data = val
        self.next = link
         
# This code is contributed by pratham76


C#
// Structure of Node
public class Node
{
    public int data;
    public Node next;
   
    // Constructor of Node
    public Node(int val, Node link = null)
    {
        this.data = val;
        this.next = link;
    }
};
 
// This code is contributed by rutvik_56


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
struct Node {
    int data;
    Node* next;
 
    // Constructor of Node
    Node(int val, Node* link = 0)
        : data(val), next(link)
    {
    }
};
 
// Create a head node
Node* head = new Node(5);
 
// Function prints the linked list
// starting from the given node
void printList(Node* n)
{
    // Till n is not NULL
    while (n != NULL) {
 
        // Print the data
        cout << n->data << " ";
        n = n->next;
    }
}
 
// Function to add a node before the
// given node other than head node
Node* addBefore(Node* given_ptr, int val)
{
 
    // First check if the given pointer
    // is the address of head
    if (head == given_ptr) {
 
        // Create a new node
        Node* n = new Node(val);
 
        // Point to next to current head
        n->next = head;
 
        // Update the head pointer
        head = n;
        return n;
    }
 
    // Otherwise traverse the list to
    // find previous node of given node
    else {
 
        Node *p, *n = head;
 
        // This loop will return p with
        // previous pointer of given node
        for (n, p; n != given_ptr;
             p = n, n = n->next)
            ;
 
        // Create a new node
        Node* m = new Node(val);
 
        // Update the m->next
        m->next = p->next;
 
        // Update previous node's next
        p->next = m;
 
        return m;
    }
}
 
// Driver Code
int main()
{
    // Head Node
    head->next = new Node(6);
 
    // Function Call
    addBefore(head->next, 8);
 
    // Print the linked List
    printList(head);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
static class Node
{
    int data;
    Node next;
     
    // Constructor of Node
    Node(int val)
    {
        this.data = val;
        this.next = null;
    }
}
 
static Node head = new Node(5);
 
// Function prints the linked list
// starting from the given node
static void printList(Node n)
{
     
    // Till n is not null
    while (n != null)
    {
         
        // Print the data
        System.out.print(n.data + " ");
        n = n.next;
    }
}
 
// Function to add a node before the
// given node other than head node
static Node addBefore(Node given_ptr, int val)
{
     
    // First check if the given pointer
    // is the address of head
    if (head == given_ptr)
    {
         
        // Create a new node
        Node n = new Node(val);
 
        // Point to next to current head
        n.next = head;
 
        // Update the head pointer
        head = n;
        return n;
    }
 
    // Otherwise traverse the list to
    // find previous node of given node
    else
    {
        Node p = null;
 
        // This loop will return p with
        // previous pointer of given node
        for(Node n = head; n != given_ptr;
                 p = n, n = n.next);
 
        // Create a new node
        Node m = new Node(val);
 
        // Update the m.next
        m.next = p.next;
 
        // Update previous node's next
        p.next = m;
 
        return m;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Head Node
    head.next = new Node(6);
 
    // Function Call
    addBefore(head.next, 8);
 
    // Print the linked List
    printList(head);
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
class Node:
     
    # Constructor of Node
    def __init__(self, val, link = None):
        self.data = val
        self.next = link
 
# Create a head node
head = Node(5);
  
# Function prints the linked list
# starting from the given node
def printList(n):
 
    # Till n is not NULL
    while (n != None):
  
        # Print the data
        print(n.data, end = ' ')
        n = n.next;
  
# Function to add a node before the
# given node other than head node
def addBefore(given_ptr, val):
    global head
     
    # First check if the given pointer
    # is the address of head
    if (head == given_ptr):
  
        # Create a node
        n = Node(val);
  
        # Point to next to current head
        n.next = head;
  
        # Update the head pointer
        head = n;
        return n;
      
    # Otherwise traverse the list to
    # find previous node of given node
    else:
  
        p = None
        n = head;
  
        # This loop will return p with
        # previous pointer of given node
        while(n != given_ptr):
            p = n
            n = n.next
  
        # Create a node
        m = Node(val);
  
        # Update the m.next
        m.next = p.next;
  
        # Update previous node's next
        p.next = m;
  
        return m;
     
# Driver Code
if __name__=='__main__':
     
    # Head Node
    head.next = Node(6);
  
    # Function Call
    addBefore(head.next, 8);
  
    # Print the linked List
    printList(head);
 
    # This code is contibuted by rutvik_56


C#
// C# program for the
// above approach
using System;
class GFG{
 
class Node
{
  public int data;
  public Node next;
 
  // Constructor of Node
  public Node(int val)
  {
    this.data = val;
    this.next = null;
  }
}
 
static Node head = new Node(5);
 
// Function prints the linked list
// starting from the given node
static void printList(Node n)
{   
  // Till n is not null
  while (n != null)
  {
    // Print the data
    Console.Write(n.data + " ");
    n = n.next;
  }
}
 
// Function to add a node before the
// given node other than head node
static Node addBefore(Node given_ptr,
                      int val)
{   
  // First check if the given
  // pointer is the address of
  // head
  if (head == given_ptr)
  {
    // Create a new node
    Node n = new Node(val);
 
    // Point to next to current
    // head
    n.next = head;
 
    // Update the head pointer
    head = n;
    return n;
  }
 
  // Otherwise traverse the list
  // to find previous node of
  // given node
  else
  {
    Node p = null;
 
    // This loop will return p with
    // previous pointer of given node
    for(Node n = head; n != given_ptr;
        p = n, n = n.next);
 
    // Create a new node
    Node m = new Node(val);
 
    // Update the m.next
    m.next = p.next;
 
    // Update previous node's next
    p.next = m;
 
    return m;
  }
}
 
// Driver Code
public static void Main(String[] args)
{   
  // Head Node
  head.next = new Node(6);
 
  // Function Call
  addBefore(head.next, 8);
 
  // Print the linked List
  printList(head);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
5 8 6

在给定的问题中,可能有两种情况:

  • 给定节点是头节点。
  • 给定节点是除头部之外的任何有效节点。

当给定的 Node 是 Head Node 时

这个想法是创建一个具有给定值K的新节点。然后新节点的下一部分将用指针头更新。最后,头部将使用新节点的地址进行更新。下面是相同的图像:

当给定的 Node 是除头节点之外的任何有效节点时

最简单的方法是遍历给定的链表来搜索给定节点的前一个节点。然后,用给定的值K创建新节点。现在,用给定节点的地址更新新节点的下一部分,用新节点的地址更新前一个节点的下一部分。下面是在图像的帮助下该方法的说明:

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
struct Node {
    int data;
    Node* next;
 
    // Constructor of Node
    Node(int val, Node* link = 0)
        : data(val), next(link)
    {
    }
};
 
// Create a head node
Node* head = new Node(5);
 
// Function prints the linked list
// starting from the given node
void printList(Node* n)
{
    // Till n is not NULL
    while (n != NULL) {
 
        // Print the data
        cout << n->data << " ";
        n = n->next;
    }
}
 
// Function to add a node before the
// given node other than head node
Node* addBefore(Node* given_ptr, int val)
{
 
    // First check if the given pointer
    // is the address of head
    if (head == given_ptr) {
 
        // Create a new node
        Node* n = new Node(val);
 
        // Point to next to current head
        n->next = head;
 
        // Update the head pointer
        head = n;
        return n;
    }
 
    // Otherwise traverse the list to
    // find previous node of given node
    else {
 
        Node *p, *n = head;
 
        // This loop will return p with
        // previous pointer of given node
        for (n, p; n != given_ptr;
             p = n, n = n->next)
            ;
 
        // Create a new node
        Node* m = new Node(val);
 
        // Update the m->next
        m->next = p->next;
 
        // Update previous node's next
        p->next = m;
 
        return m;
    }
}
 
// Driver Code
int main()
{
    // Head Node
    head->next = new Node(6);
 
    // Function Call
    addBefore(head->next, 8);
 
    // Print the linked List
    printList(head);
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
static class Node
{
    int data;
    Node next;
     
    // Constructor of Node
    Node(int val)
    {
        this.data = val;
        this.next = null;
    }
}
 
static Node head = new Node(5);
 
// Function prints the linked list
// starting from the given node
static void printList(Node n)
{
     
    // Till n is not null
    while (n != null)
    {
         
        // Print the data
        System.out.print(n.data + " ");
        n = n.next;
    }
}
 
// Function to add a node before the
// given node other than head node
static Node addBefore(Node given_ptr, int val)
{
     
    // First check if the given pointer
    // is the address of head
    if (head == given_ptr)
    {
         
        // Create a new node
        Node n = new Node(val);
 
        // Point to next to current head
        n.next = head;
 
        // Update the head pointer
        head = n;
        return n;
    }
 
    // Otherwise traverse the list to
    // find previous node of given node
    else
    {
        Node p = null;
 
        // This loop will return p with
        // previous pointer of given node
        for(Node n = head; n != given_ptr;
                 p = n, n = n.next);
 
        // Create a new node
        Node m = new Node(val);
 
        // Update the m.next
        m.next = p.next;
 
        // Update previous node's next
        p.next = m;
 
        return m;
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Head Node
    head.next = new Node(6);
 
    // Function Call
    addBefore(head.next, 8);
 
    // Print the linked List
    printList(head);
}
}
 
// This code is contributed by Amit Katiyar

蟒蛇3

# Python3 program for the above approach
class Node:
     
    # Constructor of Node
    def __init__(self, val, link = None):
        self.data = val
        self.next = link
 
# Create a head node
head = Node(5);
  
# Function prints the linked list
# starting from the given node
def printList(n):
 
    # Till n is not NULL
    while (n != None):
  
        # Print the data
        print(n.data, end = ' ')
        n = n.next;
  
# Function to add a node before the
# given node other than head node
def addBefore(given_ptr, val):
    global head
     
    # First check if the given pointer
    # is the address of head
    if (head == given_ptr):
  
        # Create a node
        n = Node(val);
  
        # Point to next to current head
        n.next = head;
  
        # Update the head pointer
        head = n;
        return n;
      
    # Otherwise traverse the list to
    # find previous node of given node
    else:
  
        p = None
        n = head;
  
        # This loop will return p with
        # previous pointer of given node
        while(n != given_ptr):
            p = n
            n = n.next
  
        # Create a node
        m = Node(val);
  
        # Update the m.next
        m.next = p.next;
  
        # Update previous node's next
        p.next = m;
  
        return m;
     
# Driver Code
if __name__=='__main__':
     
    # Head Node
    head.next = Node(6);
  
    # Function Call
    addBefore(head.next, 8);
  
    # Print the linked List
    printList(head);
 
    # This code is contibuted by rutvik_56

C#

// C# program for the
// above approach
using System;
class GFG{
 
class Node
{
  public int data;
  public Node next;
 
  // Constructor of Node
  public Node(int val)
  {
    this.data = val;
    this.next = null;
  }
}
 
static Node head = new Node(5);
 
// Function prints the linked list
// starting from the given node
static void printList(Node n)
{   
  // Till n is not null
  while (n != null)
  {
    // Print the data
    Console.Write(n.data + " ");
    n = n.next;
  }
}
 
// Function to add a node before the
// given node other than head node
static Node addBefore(Node given_ptr,
                      int val)
{   
  // First check if the given
  // pointer is the address of
  // head
  if (head == given_ptr)
  {
    // Create a new node
    Node n = new Node(val);
 
    // Point to next to current
    // head
    n.next = head;
 
    // Update the head pointer
    head = n;
    return n;
  }
 
  // Otherwise traverse the list
  // to find previous node of
  // given node
  else
  {
    Node p = null;
 
    // This loop will return p with
    // previous pointer of given node
    for(Node n = head; n != given_ptr;
        p = n, n = n.next);
 
    // Create a new node
    Node m = new Node(val);
 
    // Update the m.next
    m.next = p.next;
 
    // Update previous node's next
    p.next = m;
 
    return m;
  }
}
 
// Driver Code
public static void Main(String[] args)
{   
  // Head Node
  head.next = new Node(6);
 
  // Function Call
  addBefore(head.next, 8);
 
  // Print the linked List
  printList(head);
}
}
 
// This code is contributed by shikhasingrajput

Javascript


输出:
5 8 6

时间复杂度: O(N)
辅助空间: O(1)

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