📌  相关文章
📜  从链表的开头和结尾交换第一个奇数和偶数值的节点

📅  最后修改于: 2021-04-17 16:06:32             🧑  作者: Mango

给定一个单链表,任务是从链表的开头交换第一个奇数值节点,从结尾交换第一个偶数值节点。如果列表包含单个奇偶校验的节点值,则无需修改。

例子:

方法:可以通过分别跟踪奇数和偶数值节点的第一个和最后一次出现并交换它们来解决给定的问题。请按照以下步骤解决问题:

  • 初始化两个变量,例如firstOddfirstEven ,以存储分别从开头和结尾开始具有奇数和偶数值的第一个节点。
  • 初始化两个变量,例如firstOddfirstEven (最初为NULL)。
  • 遍历链表并执行以下步骤:
    • 如果当前节点的值为奇数,并且firstOddNULL ,则将firstOdd节点更新为当前节点。
    • 否则,将firstEven更新为当前节点。
  • 完成上述步骤后,如果firstOddfirstEven不为NULL ,则在两个指针处交换值。
  • 打印修改后的链表作为结果链表。

下面是上述方法的实现:

Python3
# Python3 program for the above approach
  
# Structure of a node
# in the Linked List
class Node:
    def __init__(self, x):
        self.val = x
        self.next = None
  
# Function to display the Linked List
def printLL(head):
  
    # Traverse until end
    # of list is reached
    while(head):
      
        # Print the value
        # stored in the head
        print(head.val, end =' ')
          
        # Move to the next of head
        head = head.next
    print()
  
# Function to swap the nodes
def swapNodes(head, even, odd):
  
    # Keeps the track of
    # prevEven and CurrEven
    prevEven = None
    currEven = head
  
    while currEven and currEven != even:
        prevEven = currEven
        currEven = currEven.next
  
    # Keeps the track of
    # prevOdd and currOdd
    prevOdd = None
    currOdd = head
  
    while currOdd and currOdd != odd:
        prevOdd = currOdd
        currOdd = currOdd.next
  
    # If list contains nodes
    # of a single parity
    if not currEven or not currOdd:
        return head
  
    # If head of the linked list
    # does not contain even value
    if prevEven:
        prevEven.next = currOdd
  
    # Make odd node the new head
    else:
        head = currOdd
  
    # If head of the linked list
    # does not contain odd value
    if prevOdd:
        prevOdd.next = currEven
  
    # Make even node the new head
    else:
        head = currEven
  
    # Swap the next pointers
    temp = currEven.next
    currEven.next = currOdd.next
    currOdd.next = temp
  
    # Return the modified Linked List
    return head
  
# Function to swap the first odd node
# from the beginning and the first even
# node from the end of the Linked List
def swapOddAndEvenNodes(head):
  
    # Find the first even node from
    # the end of the Linked List
    even = None
    curr = head
    while curr:
        if not curr.val & 1:
            even = curr
        curr = curr.next
  
    # Find the first odd node from
    # the front of the Linked List
    odd = None
    curr = head
    while curr:
        if curr.val & 1:
            odd = curr
            break
        curr = curr.next
  
    # If required odd and even
    # nodes are found, then swap
    if odd and even:
        head = swapNodes(head, even, odd)
  
    printLL(head)
  
# Function to convert given
# array into a Linked List
def linkedList(arr):
    head = None
    ptr = None
  
    # 4 -> 3 -> 5 -> 2 -> 3 -> NULL
    for i in arr:
        if not head:
            head = Node(i)
            ptr = head
        else:
            newNode = Node(i)
            ptr.next = newNode
            ptr = newNode
    return head
  
# Driver Code
  
# Given Linked List
arr = [4, 3, 5, 2, 3]
  
# Stores head of Linked List
head = linkedList(arr)
  
swapOddAndEvenNodes(head)


输出:
4 2 5 3 3

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