📌  相关文章
📜  在链表中间插入节点的Java程序

📅  最后修改于: 2022-05-13 01:57:45.110000             🧑  作者: Mango

在链表中间插入节点的Java程序

给定一个包含n 个节点的链表。问题是在列表中间插入一个带有数据x的新节点。如果n是偶数,则在第(n/2)个节点之后插入新节点,否则在第(n+1)/2个节点之后插入新节点。

例子:

Input : list: 1->2->4->5
        x = 3
Output : 1->2->3->4->5

Input : list: 5->10->4->32->16
        x = 41
Output : 5->10->4->41->32->16

方法一(使用链表的长度):
使用一次遍历查找链接的节点数或长度。让它成为len 。计算c = (len/2),如果len是偶数,否则c = (len+1)/2,如果len是奇数。再次遍历前c个节点,在第c个节点之后插入新节点。

Java
// Java implementation to insert node
// at the middle of the linked list
import java.util.*;
import java.lang.*;
import java.io.*;
  
class LinkedList
{
    static Node head; // head of list
  
    /* Node Class */
    static class Node {
        int data;
        Node next;
          
        // Constructor to create a new node
        Node(int d) {
            data = d;
            next = null;
        }
    }
  
    // function to insert node at the 
    // middle of the linked list
    static void insertAtMid(int x)
    {
        // if list is empty
        if (head == null)
            head = new Node(x);
        else {
            // get a new node
            Node newNode = new Node(x);
  
            Node ptr = head;
            int len = 0;
  
            // calculate length of the linked list
            //, i.e, the number of nodes
            while (ptr != null) {
                len++;
                ptr = ptr.next;
            }
  
            // 'count' the number of nodes after which
            // the new node is to be inserted
            int count = ((len % 2) == 0) ? (len / 2) :
                                        (len + 1) / 2;
            ptr = head;
  
            // 'ptr' points to the node after which 
            // the new node is to be inserted
            while (count-- > 1)
                ptr = ptr.next;
  
            // insert the 'newNode' and adjust 
            // the required links
            newNode.next = ptr.next;
            ptr.next = newNode;
        }
    }
  
    // function to display the linked list
    static void display()
    {
        Node temp = head;
        while (temp != null) 
        {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
    }
  
    // Driver program to test above
    public static void main (String[] args) 
    { 
        // Creating the list 1.2.4.5
        head = null;
        head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(4);
        head.next.next.next = new Node(5);
          
        System.out.println("Linked list before "+
                           "insertion: ");
        display();
  
        int x = 3;
        insertAtMid(x);
  
        System.out.println("
Linked list after"+
                           " insertion: ");
        display();
    } 
}
  
// This article is contributed by Chhavi


Java
// Java implementation to insert node 
// at the middle of the linked list
import java.util.*;
import java.lang.*;
import java.io.*;
  
class LinkedList
{
    static Node head; // head of list
  
    /* Node Class */
    static class Node {
        int data;
        Node next;
          
        // Constructor to create a new node
        Node(int d) {
            data = d;
            next = null;
        }
    }
  
    // function to insert node at the 
    // middle of the linked list
    static void insertAtMid(int x)
    {
        // if list is empty
        if (head == null)
        head = new Node(x);
  
        else {
            // get a new node
            Node newNode = new Node(x);
  
            // assign values to the slow 
            // and fast pointers
            Node slow = head;
            Node fast = head.next;
  
            while (fast != null && fast.next 
                                  != null) 
            {
                // move slow pointer to next node
                slow = slow.next;
  
                // move fast pointer two nodes 
                // at a time
                fast = fast.next.next;
            }
  
            // insert the 'newNode' and adjust 
            // the required links
            newNode.next = slow.next;
            slow.next = newNode;
        }
    }
  
    // function to display the linked list
    static void display()
    {
        Node temp = head;
        while (temp != null) 
        {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
    }
  
    // Driver program to test above
    public static void main (String[] args) 
    { 
        // Creating the list 1.2.4.5
        head = null;
        head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(4);
        head.next.next.next = new Node(5);
          
        System.out.println("Linked list before"+
                           " insertion: ");
        display();
  
        int x = 3;
        insertAtMid(x);
  
        System.out.println("
Linked list after"+
                           " insertion: ");
        display();
    } 
}
  
// This article is contributed by Chhavi


输出:

Linked list before insertion: 1 2 4 5
Linked list after insertion: 1 2 3 4 5

时间复杂度: O(n)

方法2(使用两个指针):
基于使用两个指针的龟兔算法,一个称为指针,另一个称为指针。该算法有助于找到链表的中间节点。在这篇文章的正面和黑色分裂过程中进行了解释。现在,您可以在通过上述过程获得的中间节点之后插入新节点。这种方法只需要一次遍历列表。

Java

// Java implementation to insert node 
// at the middle of the linked list
import java.util.*;
import java.lang.*;
import java.io.*;
  
class LinkedList
{
    static Node head; // head of list
  
    /* Node Class */
    static class Node {
        int data;
        Node next;
          
        // Constructor to create a new node
        Node(int d) {
            data = d;
            next = null;
        }
    }
  
    // function to insert node at the 
    // middle of the linked list
    static void insertAtMid(int x)
    {
        // if list is empty
        if (head == null)
        head = new Node(x);
  
        else {
            // get a new node
            Node newNode = new Node(x);
  
            // assign values to the slow 
            // and fast pointers
            Node slow = head;
            Node fast = head.next;
  
            while (fast != null && fast.next 
                                  != null) 
            {
                // move slow pointer to next node
                slow = slow.next;
  
                // move fast pointer two nodes 
                // at a time
                fast = fast.next.next;
            }
  
            // insert the 'newNode' and adjust 
            // the required links
            newNode.next = slow.next;
            slow.next = newNode;
        }
    }
  
    // function to display the linked list
    static void display()
    {
        Node temp = head;
        while (temp != null) 
        {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
    }
  
    // Driver program to test above
    public static void main (String[] args) 
    { 
        // Creating the list 1.2.4.5
        head = null;
        head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(4);
        head.next.next.next = new Node(5);
          
        System.out.println("Linked list before"+
                           " insertion: ");
        display();
  
        int x = 3;
        insertAtMid(x);
  
        System.out.println("
Linked list after"+
                           " insertion: ");
        display();
    } 
}
  
// This article is contributed by Chhavi

输出:

Linked list before insertion: 1 2 4 5
Linked list after insertion: 1 2 3 4 5

时间复杂度: O(n)

详情请参考完整文章将节点插入链表中间!