📌  相关文章
📜  用于在从末尾开始的第 N 个节点之后插入一个节点的Python程序

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

用于在从末尾开始的第 N 个节点之后插入一个节点的Python程序

在给定的单链表中,在倒数第 n 个节点之后插入一个节点x 。保证列表包含倒数第 n 个节点。还有 1 <= n。

例子:

Input : list: 1->3->4->5
        n = 4, x = 2
Output : 1->2->3->4->5
4th node from the end is 1 and
insertion has been done after this node.

Input : list: 10->8->3->12->5->18
        n = 2, x = 11
Output : 10->8->3->12->5->11->18

方法1(使用列表的长度):
求链表的长度,即链表中的节点数。让它成为len 。现在从第一个节点遍历列表到第(len-n+1) 个节点,并在该节点之后插入新节点。此方法需要对列表进行两次遍历。

Python3
# Python implementation to insert a node after 
# the n-th node from the end 
  
# Linked List node 
class Node: 
    def __init__(self, data): 
        self.data = data 
        self.next = None
  
# function to get a new node 
def getNode(data) :
  
    # allocate memory for the node 
    newNode = Node(0) 
  
    # put in the data 
    newNode.data = data 
    newNode.next = None
    return newNode 
  
# function to insert a node after the 
# nth node from the end 
def insertAfterNthNode(head, n, x) :
  
    # if list is empty 
    if (head == None) :
        return
  
    # get a new node for the value 'x' 
    newNode = getNode(x) 
    ptr = head 
    len = 0
    i = 0
  
    # find length of the list, i.e, the 
    # number of nodes in the list 
    while (ptr != None) :
      
        len = len + 1
        ptr = ptr.next
      
    # traverse up to the nth node from the end 
    ptr = head 
    i = 1
    while ( i <= (len - n) ) :
        ptr = ptr.next
        i = i + 1
  
    # insert the 'newNode' by making the 
    # necessary adjustment in the links 
    newNode.next = ptr.next
    ptr.next = newNode 
  
# function to print the list 
def printList( head) :
  
    while (head != None):
      
        print(head.data ,end = " ") 
        head = head.next
      
# Driver code 
  
# Creating list 1->3->4->5 
head = getNode(1) 
head.next = getNode(3) 
head.next.next = getNode(4) 
head.next.next.next = getNode(5) 
  
n = 4
x = 2
  
print("Original Linked List: ") 
printList(head) 
  
insertAfterNthNode(head, n, x) 
print()
print("Linked List After Insertion: ") 
printList(head) 
  
# This code is contributed by Arnab Kundu


Python3
# Python3 implementation to insert a
# node after the nth node from the end
   
# Structure of a node
class Node:
      
    def __init__(self, data):
          
        self.data = data
        self.next = None
      
# Function to get a new node
def getNode(data):
      
    # Allocate memory for the node
    newNode = Node(data)
    return newNode
  
# Function to insert a node after the
# nth node from the end
def insertAfterNthNode(head, n, x):
  
    # If list is empty
    if (head == None):
        return
   
    # Get a new node for the value 'x'
    newNode = getNode(x)
   
    # Initializing the slow and fast pointers
    slow_ptr = head
    fast_ptr = head
   
    # Move 'fast_ptr' to point to the nth
    # node from the beginning
    for i in range(1, n):
        fast_ptr = fast_ptr.next
   
    # Iterate until 'fast_ptr' points to the
    # last node
    while (fast_ptr.next != None):
   
        # Move both the pointers to the
        # respective next nodes
        slow_ptr = slow_ptr.next
        fast_ptr = fast_ptr.next
  
    # Insert the 'newNode' by making the
    # necessary adjustment in the links
    newNode.next = slow_ptr.next
    slow_ptr.next = newNode
  
# Function to print the list
def printList(head):
  
    while (head != None):
        print(head.data, end = ' ')
        head = head.next
      
# Driver code
if __name__=='__main__':
      
    # Creating list 1.3.4.5
    head = getNode(1)
    head.next = getNode(3)
    head.next.next = getNode(4)
    head.next.next.next = getNode(5)
   
    n = 4
    x = 2
   
    print("Original Linked List: ", end = '')
    printList(head)
   
    insertAfterNthNode(head, n, x)
   
    print("
Linked List After Insertion: ", end = '')
    printList(head)
   
# This code is contributed by rutvik_56


输出:

Original Linked List: 1 3 4 5
Linked List After Insertion: 1 2 3 4 5

时间复杂度: O(n),其中n是列表中的节点数。

方法二(单次遍历):
该方法使用两个指针,一个是slow_ptr ,另一个是fast_ptr 。首先将fast_ptr 从头向上移动到第n 个节点。使slow_ptr指向列表的第一个节点。现在,同时移动两个指针,直到fast_ptr指向最后一个节点。此时, slow_ptr将指向从末尾算起的第 n 个节点。在该节点之后插入新节点。此方法需要单次遍历列表。

Python3

# Python3 implementation to insert a
# node after the nth node from the end
   
# Structure of a node
class Node:
      
    def __init__(self, data):
          
        self.data = data
        self.next = None
      
# Function to get a new node
def getNode(data):
      
    # Allocate memory for the node
    newNode = Node(data)
    return newNode
  
# Function to insert a node after the
# nth node from the end
def insertAfterNthNode(head, n, x):
  
    # If list is empty
    if (head == None):
        return
   
    # Get a new node for the value 'x'
    newNode = getNode(x)
   
    # Initializing the slow and fast pointers
    slow_ptr = head
    fast_ptr = head
   
    # Move 'fast_ptr' to point to the nth
    # node from the beginning
    for i in range(1, n):
        fast_ptr = fast_ptr.next
   
    # Iterate until 'fast_ptr' points to the
    # last node
    while (fast_ptr.next != None):
   
        # Move both the pointers to the
        # respective next nodes
        slow_ptr = slow_ptr.next
        fast_ptr = fast_ptr.next
  
    # Insert the 'newNode' by making the
    # necessary adjustment in the links
    newNode.next = slow_ptr.next
    slow_ptr.next = newNode
  
# Function to print the list
def printList(head):
  
    while (head != None):
        print(head.data, end = ' ')
        head = head.next
      
# Driver code
if __name__=='__main__':
      
    # Creating list 1.3.4.5
    head = getNode(1)
    head.next = getNode(3)
    head.next.next = getNode(4)
    head.next.next.next = getNode(5)
   
    n = 4
    x = 2
   
    print("Original Linked List: ", end = '')
    printList(head)
   
    insertAfterNthNode(head, n, x)
   
    print("
Linked List After Insertion: ", end = '')
    printList(head)
   
# This code is contributed by rutvik_56

输出:

Original Linked List: 1 3 4 5
Linked List After Insertion: 1 2 3 4 5

时间复杂度: O(n),其中n是列表中的节点数。

有关详细信息,请参阅关于在倒数第 n 个节点之后插入节点的完整文章!