📜  须藤放置[1.4] |总和

📅  最后修改于: 2021-05-30 07:26:09             🧑  作者: Mango

给定整数和整数k的链表的开头,您的任务是按如下方式修改链表:

  • 考虑大小为k的组中的节点。在每个组中,将第一个节点的值替换为组和。
  • 另外,删除组中除第一个节点之外的元素。
  • 在遍历过程中,如果链表中的其余节点小于k,则还要考虑剩余节点,执行上述操作。

例子:

方法:在链接列表中插入给定的节点。这篇文章已经讨论了插入方法。插入节点后,请从列表的开头进行迭代。将第一个节点标记为临时节点。循环访问下一个k-1个节点,并对sum变量中的节点求和。如果节点数小于K,则将温度节点的数据替换为sum,并将temp指向NULL。如果存在一个包含K个节点的组,请用和替换temp的数据,然后将temp移到K个节点之后的节点。继续上述操作,直到temp指向NULL。一旦temp到达末尾,则意味着遍历了所有大小为K的组,并且已用大小为K的节点总和替换了该节点。一旦完成了替换操作,就可以打印由此形成的链表。在这篇文章中已经讨论了打印链接列表的方法。

下面是上述方法的实现:

C++
// C++ program for
// SP - K Sum
  
#include 
using namespace std;
  
// structure for linked list
struct Node {
    long long data;
    Node* next;
};
  
// allocated new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->next = NULL;
    return temp;
}
  
// function to insert node in a Linked List
Node* insertNode(Node* head, int key)
{
    if (head == NULL)
        head = newNode(key);
    else
        head->next = insertNode(head->next, key);
  
    return head;
}
  
// traverse the node
// to print it
void print(Node* head)
{
    // travere the linked list
    while (head) {
        cout << head->data << " ";
        head = head->next;
    }
    cout << endl;
}
  
// function to perform the following operation
void KSum(Node* head, int k)
{
    // initially pointing to start
    Node* temp = head;
  
    // iterate till end
    while (temp) {
  
        // dummy variable to store k
        long long count = k;
  
        // summation of nodes
        long long sum = 0;
  
        // currently at node from
        // which itearation is done
        Node* curr = temp;
  
        // till k nodes are visited and
        // last node is not visited
        while (count-- && curr) {
            // add to sum the node data
            sum = sum + curr->data;
  
            // move to the next node
            curr = curr->next;
        }
  
        // change pointer's data of temp to sum
        temp->data = sum;
  
        // move temp to next pointer
        temp->next = curr;
  
        // move temp to the next pointer
        temp = temp->next;
    }
}
  
// Driver Code
int main()
{
  
    Node* head = NULL;
  
    // inserts nodes in the linked list
    head = insertNode(head, 1);
    head = insertNode(head, 2);
    head = insertNode(head, 3);
    head = insertNode(head, 4);
    head = insertNode(head, 5);
    head = insertNode(head, 6);
  
    int k = 2;
    // Function call to perform the
    // given operations
    KSum(head, k);
  
    // print the linked list
    print(head);
  
    return 0;
}


Java
// Java program for
// SP - K Sum
  
import java.io.*;
import java.util.*;
  
// structure for linked list
class Node 
{
    int data;
    Node next;
    Node(int key)
    {
        data = key;
        next = null;
    }
}
  
class GFG
{
      
// allocated new node
static Node head;
  
// function to insert node
// in a Linked List
public static Node insertNode(Node head, int key)
{
    if (head == null)
        head = new Node(key);
    else
        head.next = insertNode(head.next, key);
  
    return head;
}
  
// traverse the node
// to print it
public static void print(Node head)
{
    // travere the linked list
    while (head != null) 
    {
        System.out.print(head.data + " ");
        head = head.next;
    }
    System.out.println();
}
  
// function to perform 
// the following operation
public static void KSum(Node head, int k)
{
    // initially pointing
    // to start
    Node temp = head;
  
    // iterate till end
    while (temp != null) 
    {
  
        // dummy variable
        // to store k
        int count = k;
  
        // summation of nodes
        int sum = 0;
  
        // currently at node from
        // which itearation is done
        Node curr = temp;
  
        // till k nodes are visited and
        // last node is not visited
        while (count > 0 && curr != null) 
        {
            // add to sum the node data
            sum = sum + curr.data;
  
            // move to the next node
            curr = curr.next;
            count--;
        }
  
        // change pointer's data
        // of temp to sum
        temp.data = sum;
  
        // move temp to
        // next pointer
        temp.next = curr;
  
        // move temp to 
        // the next pointer
        temp = temp.next;
    }
      
    //return temp;
}
  
// Driver Code
public static void main(String args[])
{
     head = null;
  
    // inserts nodes in
    // the linked list
    head = insertNode(head, 1);
    head = insertNode(head, 2);
    head = insertNode(head, 3);
    head = insertNode(head, 4);
    head = insertNode(head, 5);
    head = insertNode(head, 6);
  
    int k = 2;
      
    // Function call to perform 
    // the given operations
     KSum(head, k);
  
    // print the linked list
    print(head);
}
}


Python3
# Python program for
# SP - K Sum
  
# structure for linked list
class Node:
    def __init__(self, key):
        self.data = key
        self.next = None
  
# function to insert node in a Linked List
def insertNode(head: Node, key: int) -> Node:
    if head is None:
        head = Node(key)
    else:
        head.next = insertNode(head.next, key)
  
    return head
  
# traverse the node
# to print it
def Print(head: Node):
  
    # travere the linked list
    while head:
        print(head.data, end = " ")
        head = head.next
    print()
  
# function to perform the following operation
def KSum(head: Node, k: int):
  
    # initially pointing to start
    temp = head
  
    # iterate till end
    while temp:
  
        # dummy variable to store k
        count = k
  
        # summation of nodes
        sum = 0
  
        # currently at node from
        # which itearation is done
        curr = temp
  
        # till k nodes are visited and
        # last node is not visited
        while count and curr:
  
            # add to sum the node data
            sum = sum + curr.data
  
            # move to the next node
            curr = curr.next
            count -= 1
  
        # change pointer's data of temp to sum
        temp.data = sum
  
        # move temp to next pointer
        temp.next = curr
  
        # move temp to the next pointer
        temp = temp.next
  
# Driver Code
if __name__ == "__main__":
    head = None
  
    # inserts nodes in the linked list
    head = insertNode(head, 1)
    head = insertNode(head, 2)
    head = insertNode(head, 3)
    head = insertNode(head, 4)
    head = insertNode(head, 5)
    head = insertNode(head, 6)
  
    k = 2
  
    # Function call to perform the
    # given operations
    KSum(head, k)
  
    # print the linked list
    Print(head)
  
# This code is contributed by
# sanjeev2552


C#
// C# program for SP - K Sum
using System;
  
// structure for linked list
public class Node 
{
    public int data;
    public Node next;
    public Node(int key)
    {
        data = key;
        next = null;
    }
}
  
public class GFG
{
      
// allocated new node
static Node head;
  
// function to insert node
// in a Linked List
public static Node insertNode(Node head, int key)
{
    if (head == null)
        head = new Node(key);
    else
        head.next = insertNode(head.next, key);
  
    return head;
}
  
// traverse the node
// to print it
public static void print(Node head)
{
    // travere the linked list
    while (head != null) 
    {
        Console.Write(head.data + " ");
        head = head.next;
    }
    Console.WriteLine();
}
  
// function to perform 
// the following operation
public static void KSum(Node head, int k)
{
    // initially pointing
    // to start
    Node temp = head;
  
    // iterate till end
    while (temp != null) 
    {
  
        // dummy variable
        // to store k
        int count = k;
  
        // summation of nodes
        int sum = 0;
  
        // currently at node from
        // which itearation is done
        Node curr = temp;
  
        // till k nodes are visited and
        // last node is not visited
        while (count > 0 && curr != null) 
        {
            // add to sum the node data
            sum = sum + curr.data;
  
            // move to the next node
            curr = curr.next;
            count--;
        }
  
        // change pointer's data
        // of temp to sum
        temp.data = sum;
  
        // move temp to
        // next pointer
        temp.next = curr;
  
        // move temp to 
        // the next pointer
        temp = temp.next;
    }
    // return temp;
}
  
// Driver Code
public static void Main()
{
    head = null;
  
    // inserts nodes in
    // the linked list
    head = insertNode(head, 1);
    head = insertNode(head, 2);
    head = insertNode(head, 3);
    head = insertNode(head, 4);
    head = insertNode(head, 5);
    head = insertNode(head, 6);
  
    int k = 2;
      
    // Function call to perform 
    // the given operations
    KSum(head, k);
  
    // print the linked list
    print(head);
}
}
  
/* This code contributed by PrinciRaj1992 */


输出:

3 7 11
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”