热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

生成链表,由给定链表中节点对的最大平方差组成

生成链表,由给定链表中节点对的最大平方差组成原文:https://www.geeksforgeeks.org/generate-

生成链表,由给定链表中节点对的最大平方差组成

原文:https://www.geeksforgeeks.org/generate-linked-list-consisting-of-maximum-difference-of-squares-of-pairs-of-nodes-from-given-linked-list/

给定节点数为偶数的链表,任务是生成一个新的链表,使其以降序包含节点值的最大平方差,通过将每个节点包括在偶对中。

示例

输入1 -> 6 -> 4 -> 3 -> 5 -> 2

输出35 -> 21 -> 7

说明

6 和 1 的平方差形成值为 35 的第一个节点。

5 和 2 的平方差​​构成具有值 21 的第二个节点。

4 和 3 的平方之差形成具有值 7 的第三个节点。

因此,形成的链表为35 -> 21 -> 7

输入2 -> 4 -> 5 -> 3 -> 7 -> 8 -> 9 -> 10

输出96 -> 72 -> 48 -> 10

说明

10 和 2 的平方之差形成值为 96 的第一个节点。

9 和 3 的平方之差形成值为 72 的第二个节点。

8 和 4 的平方之差形成值为 48 的第三个节点。

7 和 5 的平方之差形成值为 10 的第四个节点。

因此,形成的链表为96 -> 72 -> 48 -> 10

方法:方法是找到节点的最大值,并始终使最大节点和最小节点之间的。 因此,创建一个双端队列 并在其中插入所有节点值,然后对双端队列排序。 现在,从两端访问最大值和最小值。 步骤如下:


  • 创建双端队列,并将所有节点值插入双端队列。


  • 对双端队列排序,以在恒定时间内获得最大的节点值和最小的节点值。


  • 创建另一个链表,其值与双端队列的后面和前面的平方和之差最大。


  • 在每次迭代之后,从双端队列弹出最小值和最大值。


  • 完成上述步骤后,打印形成的新链表的节点。


下面是上述方法的实现:

C++

// C++ program for the above approach
#include
using namespace std;
// Linked list node
struct Node {
    int data;
    struct Node* next;
};
// Function to push into Linked List
void push(struct Node** head_ref,
          int new_data)
{
    // Allocate node
    struct Node* new_node
        = (struct Node*)malloc(
            sizeof(struct Node));
    // Put in the data
    new_node->data = new_data;
    new_node->next = (*head_ref);
    // Move the head to point
    // to the new node
    (*head_ref) = new_node;
}
// Function to print the Linked List
void print(struct Node* head)
{
    Node* curr = head;
    // Iterate until curr is NULL
    while (curr) {
        // Print the data
        cout <data <<" ";
        // Move to next
        curr = curr->next;
    }
}
// Function to create a new Node of
// the Linked List
struct Node* newNode(int x)
{
    struct Node* temp
        = (struct Node*)malloc(
            sizeof(struct Node));
    temp->data = x;
    temp->next = NULL;
    // Return the node created
    return temp;
}
// Function used to re-order list
struct Node* reorder(Node* head)
{
    // Stores the node of LL
    deque v;
    Node* curr = head;
    // Traverse the LL
    while (curr) {
        v.push_back(curr->data);
        curr = curr->next;
    }
    // Sort the deque
    sort(v.begin(), v.end());
    // Node head1 stores the
    // head of the new Linked List
    Node* head1 = NULL;
    Node* prev = NULL;
    // Size of new LL
    int x = v.size() / 2;
    // Loop to make new LL
    while (x--) {
        int a = v.front();
        int b = v.back();
        // Difference of squares of
        // largest and smallest value
        int ans = pow(b, 2) - pow(a, 2);
        // Create node with value ans
        struct Node* temp = newNode(ans);
        if (head1 == NULL) {
            head1 = temp;
            prev = temp;
        }
        // Otherwsie, update prev
        else {
            prev->next = temp;
            prev = temp;
        }
        // Pop the front and back node
        v.pop_back();
        v.pop_front();
    }
    // Return head of the new LL
    return head1;
}
// Driver Code
int main()
{
    struct Node* head = NULL;
    // Given Linked ist
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
    // Function Call
    Node* temp = reorder(head);
    // Print the new LL formed
    print(temp);
    return 0;
}

Java

// Java program for the
// above approach
import java.util.*;
class GFG{
// Linked list node
static class Node
{
  int data;
  Node next;
};
static Node head ;
// Function to push
// into Linked List
static void push(int new_data)
{
  // Allocate node
  Node new_node = new Node();
  // Put in the data
  new_node.data = new_data;
  new_node.next = head;
  // Move the head to point
  // to the new node
  head = new_node;
}
// Function to print the
// Linked List
static void print(Node head)
{
  Node curr = head;
  // Iterate until curr
  // is null
  while (curr != null)
  {
    // Print the data
    System.out.print(curr.data + " ");
    // Move to next
    curr = curr.next;
  }
}
// Function to create a
// new Node of the Linked List
static Node newNode(int x)
{
  Node temp = new Node();
  temp.data = x;
  temp.next = null;
  // Return the node
  // created
  return temp;
}
// Function used to re-order
// list
static Node reorder(Node head)
{
  // Stores the node of LL
  Deque v =
        new LinkedList<>();
  Node curr = head;
  // Traverse the LL
  while (curr != null)
  {
    v.add(curr.data);
    curr = curr.next;
  }
  // Sort the deque
  // Collections.sort(v);
  // Node head1 stores the
  // head of the new Linked
  // List
  Node head1 = null;
  Node prev = null;
  // Size of new LL
  int x = v.size() / 2;
  // Loop to make new LL
  while ((x--) > 0)
  {
    int a = v.peek();
    int b = v.getLast();
    // Difference of squares of
    // largest and smallest value
    int ans = (int)(Math.pow(b, 2) -
                    Math.pow(a, 2));
    // Create node with value ans
    Node temp = newNode(ans);
    if (head1 == null)
    {
      head1 = temp;
      prev = temp;
    }
    // Otherwsie, update prev
    else
    {
      prev.next = temp;
      prev = temp;
    }
    // Pop the front and
    // back node
    v.removeFirst();
    v.removeLast();
  }
  // Return head of the
  // new LL
  return head1;
}
// Driver Code
public static void main(String[] args)
{
  head = null;
  // Given Linked ist
  push(6);
  push(5);
  push(4);
  push(3);
  push(2);
  push(1);
  // Function Call
  Node temp = reorder(head);
  // Print the new
  // LL formed
  print(temp);
}
}
// This code is contributed by Amit Katiyar

C

// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
// Linked list node
public class Node
{
  public int data;
  public Node next;
};
static Node head ;
// Function to push
// into Linked List
static void push(int new_data)
{
  // Allocate node
  Node new_node = new Node();
  // Put in the data
  new_node.data = new_data;
  new_node.next = head;
  // Move the head to point
  // to the new node
  head = new_node;
}
// Function to print the
// Linked List
static void print(Node head)
{
  Node curr = head;
  // Iterate until curr
  // is null
  while (curr != null)
  {
    // Print the data
    Console.Write(curr.data + " ");
    // Move to next
    curr = curr.next;
  }
}
// Function to create a
// new Node of the Linked List
static Node newNode(int x)
{
  Node temp = new Node();
  temp.data = x;
  temp.next = null;
  // Return the node
  // created
  return temp;
}
// Function used to re-order
// list
static Node reorder(Node head)
{
  // Stores the node of LL
  List v =
       new List();    
  Node curr = head;
  // Traverse the LL
  while (curr != null)
  {
    v.Add(curr.data);
    curr = curr.next;
  }
  // Sort the deque
  // Collections.sort(v);
  // Node head1 stores the
  // head of the new Linked
  // List
  Node head1 = null;
  Node prev = null;
  // Size of new LL
  int x = v.Count / 2;
  // Loop to make new LL
  while ((x--) > 0)
  {
    int a = v[0];
    int b = v[v.Count-1];
    // Difference of squares of
    // largest and smallest value
    int ans = (int)(Math.Pow(b, 2) -
                    Math.Pow(a, 2));
    // Create node with value ans
    Node temp = newNode(ans);
    if (head1 == null)
    {
      head1 = temp;
      prev = temp;
    }
    // Otherwsie, update prev
    else
    {
      prev.next = temp;
      prev = temp;
    }
    // Pop the front and
    // back node
    v.RemoveAt(0);
    v.RemoveAt(v.Count - 1);
  }
  // Return head of the
  // new LL
  return head1;
}
// Driver Code
public static void Main(String[] args)
{
  head = null;
  // Given Linked ist
  push(6);
  push(5);
  push(4);
  push(3);
  push(2);
  push(1);
  // Function Call
  Node temp = reorder(head);
  // Print the new
  // LL formed
  print(temp);
}
}
// This code is contributed by gauravrajput1

输出: 

35 21 7

时间复杂度O(N * log N)

辅助空间O(n)




如果您喜欢 GeeksforGeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 GeeksforGeeks 主页上,并帮助其他 Geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
author-avatar
手机用户2602940163
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有