📜  在链表中找到平衡节点

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

在链表中找到平衡节点

给定一个链表,任务是在链表中找到平衡节点。平衡节点是其左侧所有节点的总和等于其右侧所有节点的总和的节点,如果找不到这样的节点,则打印-1

例子:

方法:



  • 首先,找到所有节点值的总和。
  • 现在,逐一遍历链表,在遍历的同时跟踪所有先前节点的值和,并通过从总和中减去当前节点值和先前节点值之和来找到剩余节点的总和。
  • 比较两个总和,如果它们相等,则当前节点是所需的节点,否则打印 -1。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Structure of a node of linked list
class Node {
public:
    int data;
    Node* next;
    Node(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};
  
// Push the new node to front of
// the linked list
Node* push(Node* head, int data)
{
      
    // Return new node as head if
    // head is empty
    if (head == NULL)
    {
        return new Node(data);
    }
    Node* temp = new Node(data);
    temp->next = head;
    head = temp;
    return head;
}
  
// Function to find the balanced node
int findBalancedNode(Node* head)
{
    int tsum = 0;
    Node* curr_node = head;
      
    // Traverse through all node
    // to find the total sum
    while (curr_node != NULL)
    {
        tsum += curr_node->data;
        curr_node = curr_node->next;
    }
  
    // Set current_sum and remaining
    // sum to zero
    int current_sum = 0;
    int remaining_sum = 0;
    curr_node = head;
  
    // Traversing the list to
    // check balanced node
    while (curr_node != NULL)
    {
        remaining_sum = tsum - (current_sum +
                               curr_node->data);
  
        // If sum of the nodes on the left and
        // the current node is equal to the sum
        // of the nodes on the right
        if (current_sum == remaining_sum)
        {
            return curr_node->data;
        }
        current_sum += curr_node->data;
        curr_node = curr_node->next;
    }
    return -1;
}
 
// Driver code
int main()
{
    Node* head = NULL;
    head = push(head, 3);
    head = push(head, 6);
    head = push(head, 1);
    head = push(head, 10);
    head = push(head, 7);
    head = push(head, 2);
    head = push(head, 1);
    cout << findBalancedNode(head);
    return 0;
}
 
// This code is contributed by divyehrabadiya07


Java
// Java implementation of the approach
class GFG{
     
// Structure of a node of linked list
static class Node
{
    int data;
    Node next;
     
    Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}
 
// Push the new node to front of
// the linked list
static Node push(Node head, int data)
{
     
    // Return new node as head if
    // head is empty
    if (head == null)
    {
        return new Node(data);
    }
    Node temp = new Node(data);
    temp.next = head;
    head = temp;
    return head;
}
 
// Function to find the balanced node
static int findBalancedNode(Node head)
{
    int tsum = 0;
    Node curr_node = head;
     
    // Traverse through all node
    // to find the total sum
    while (curr_node != null)
    {
        tsum += curr_node.data;
        curr_node = curr_node.next;
    }
 
    // Set current_sum and remaining
    // sum to zero
    int current_sum = 0;
    int remaining_sum = 0;
    curr_node = head;
 
    // Traversing the list to
    // check balanced node
    while (curr_node != null)
    {
        remaining_sum = tsum - (current_sum +
                               curr_node.data);
 
        // If sum of the nodes on the left and
        // the current node is equal to the sum
        // of the nodes on the right
        if (current_sum == remaining_sum)
        {
            return curr_node.data;
        }
        current_sum += curr_node.data;
        curr_node = curr_node.next;
    }
    return -1;
}
 
// Driver code
public static void main(String []args)
{
    Node head = null;
    head = push(head, 3);
    head = push(head, 6);
    head = push(head, 1);
    head = push(head, 10);
    head = push(head, 7);
    head = push(head, 2);
    head = push(head, 1);
 
    System.out.println(findBalancedNode(head));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 implementation of the approach
import sys
import math
 
# Structure of a node of linked list
class Node:
    def __init__(self, data):
        self.next = None
        self.data = data
 
# Push the new node to front of the linked list
def push(head, data):
 
    # Return new node as head if head is empty
    if not head:
        return Node(data)
    temp = Node(data)
    temp.next = head
    head = temp
    return head
 
# Function to find the balanced node
def findBalancedNode(head):
    tsum = 0
    curr_node = head
     
    # Traverse through all node
    # to find the total sum
    while curr_node:
        tsum+= curr_node.data
        curr_node = curr_node.next
     
    # Set current_sum and remaining sum to zero
    current_sum, remaining_sum = 0, 0
    curr_node = head
 
    # Traversing the list to check balanced node
    while(curr_node):
        remaining_sum = tsum-(current_sum + curr_node.data)
 
        # If sum of the nodes on the left and the current node
        # is equal to the sum of the nodes on the right
        if current_sum == remaining_sum:
            return curr_node.data
        current_sum+= curr_node.data
        curr_node = curr_node.next
     
    return -1
 
# Driver code
if __name__=='__main__':
    head = None
    head = push(head, 3)
    head = push(head, 6)
    head = push(head, 1)
    head = push(head, 10)
    head = push(head, 7)
    head = push(head, 2)
    head = push(head, 1)
 
    print(findBalancedNode(head))


C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
 
  // Structure of a node of linked list
  class Node
  {
    public int data;
    public Node next;
 
    public Node(int data)
    {
      this.data = data;
      this.next = null;
    }
  }
 
  // Push the new node to front of
  // the linked list
  static Node push(Node head, int data)
  {
 
    // Return new node as head if
    // head is empty
    if (head == null)
    {
      return new Node(data);
    }
    Node temp = new Node(data);
    temp.next = head;
    head = temp;
    return head;
  }
 
  // Function to find the balanced node
  static int findBalancedNode(Node head)
  {
    int tsum = 0;
    Node curr_node = head;
 
    // Traverse through all node
    // to find the total sum
    while (curr_node != null)
    {
      tsum += curr_node.data;
      curr_node = curr_node.next;
    }
 
    // Set current_sum and remaining
    // sum to zero
    int current_sum = 0;
    int remaining_sum = 0;
    curr_node = head;
 
    // Traversing the list to
    // check balanced node
    while (curr_node != null)
    {
      remaining_sum = tsum - (current_sum +
                              curr_node.data);
 
      // If sum of the nodes on the left and
      // the current node is equal to the sum
      // of the nodes on the right
      if (current_sum == remaining_sum)
      {
        return curr_node.data;
      }
      current_sum += curr_node.data;
      curr_node = curr_node.next;
    }
    return -1;
  }
 
  // Driver code
  public static void Main(string []args)
  {
    Node head = null;
    head = push(head, 3);
    head = push(head, 6);
    head = push(head, 1);
    head = push(head, 10);
    head = push(head, 7);
    head = push(head, 2);
    head = push(head, 1);
    Console.Write(findBalancedNode(head));
  }
}
 
// This code is contributed by pratham76


Javascript



输出:
10

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程