25. Reverse Nodes in k-Group 还未解决!!!

本文介绍了一种算法,该算法将链表中的节点每K个一组进行反转,并返回修改后的链表。该方法不改变节点的值,只改变节点本身的位置,且仅使用常数级内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5


public ListNode reverseKGroup(ListNode head, int k) {
    ListNode curr = head;
    int count = 0;
    while (curr != null && count != k) { // find the k+1 node
        curr = curr.next;
        count++;
    }
    if (count == k) { // if k+1 node is found
        curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
        // head - head-pointer to direct part, 
        // curr - head-pointer to reversed part;
        while (count-- > 0) { // reverse current k-group: 
            ListNode tmp = head.next; // tmp - next head in direct part
            head.next = curr; // preappending "direct" head to the reversed list 
            curr = head; // move head of reversed part to a new node
            head = tmp; // move "direct" head to the next node in direct part
        }
        head = curr;
    }
    return head;
}

# 导入库函数 import networkx as nx from random import * import numpy as np import pandas as pd # 定义初始负载 def initial_load(G, a): initial = [] for node in G.nodes(): initial.append(pow(G.degree(node), a)) initial_load = dict(zip(G.nodes(), initial)) return initial_load # 定义节点容量 def volume(G, b, initial_load): vol = [] for node in G.nodes(): v = (1 + b) * initial_load[node] vol.append(v) volume = dict(zip(G.nodes(), vol)) return volume # 定义重分配函数 def reload(G, fore_load, a2, fail_node): # 计算重分配负载 total = 0 for j in G.neighbors(fail_node): cj = pow(G.degree(j), a2) total += cj for j in G.neighbors(fail_node): reload_i_to_j = fore_load[fail_node] * pow(G.degree(j), a2) / total fore_load[j] = fore_load[j] + reload_i_to_j fore_load.pop(fail_node) return fore_load # 定义级联失效函数 def cascade_failure(G, a, a2, fail_node): G_cascade = G.copy() b_list_ = np.linspace(0, 1.5, 151) b_list = [] for i in b_list_: b_list.append(round(i, 3)) b_list.reverse() for b in b_list: bm = 0 fore_load = initial_load(G_cascade, a).copy() nodes_volume = volume(G_cascade, b, fore_load) present_load = reload(G_cascade, fore_load, a2, fail_node) for i in present_load.keys(): if present_load[i] > nodes_volume[i]: bm = b break if bm != 0: break return bm # 阈值分析 def bm_analyze(G, a, attacted_node): a2_list_ = np.linspace(0, 10, 101) a2_list = [] for i in a2_list_: a2_list.append(round(i, 3)) # 二进制与十进制转换时会有一定误差,有些数会有小尾巴,这里限制其位数来避免这种情况 # 创建空的DataFrame用以保存数据 index = a2_list bm_data = pd.DataFrame(index=index) bm_list = [] for a2 in a2_list: bm_list.append(cascade_failure(G, a, a2, attacted_node)) bm_data[0] = bm_list # 保存数据 bm_data.to_csv("bm_0.7.csv") def load_network(adj_path, comm_path): adj_matrix = np.loadtxt(adj_path, delimiter=',') G = nx.from_numpy_array(adj_matrix) comm_df = pd.read_csv(comm_path) return G, comm_df['lambda'].values, comm_df['community'].values, comm_df['Ni'].values ADJ_FILE = "network_group0_exp7_adj.csv" COMM_FILE = "network_group0_exp7_comm.csv" G, lambda_i, community, Ni = load_network(ADJ_FILE, COMM_FILE) a = 1 # 获取攻击节点 attacted_node = max(G.degree, key=lambda x: x[1]) bm_analyze(G, a, attacted_node[0]) 这段代码,改变他的再分配策略,考虑社团,def reload(G, fore_load, a2, fail_node,community,x),失效节点负载的x倍分配给与失效节点编号community[i]相同的邻居,1-x倍分配给不同的社团的邻居节点
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值