725. Split Linked List in Parts

本文介绍了一种将单链表平均分成k个连续子链表的算法实现。该算法确保了每个子链表长度尽可能相等,且相邻部分大小差不超过1。通过实例展示了输入输出的转换过程及具体步骤。

Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts".

The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may lead to some parts being null.

The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.

Return a List of ListNode's representing the linked list parts that are formed.

Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]

Example 1:

Input:  root = [1, 2, 3], k = 5 Output: [[1],[2],[3],[],[]] Explanation: The input and each element of the output are ListNodes, not arrays. For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null. The first element output[0] has output[0].val = 1, output[0].next = null. The last element output[4] is null, but it's string representation as a ListNode is []. 

Example 2:

Input: 
root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.

Note:

The length of root will be in the range [0, 1000].Each value of a node in the input will be an integer in the range [0, 999].k will be an integer in the range [1, 50].


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        int len = 0;
        ListNode * tmp = root;
        while(tmp!=NULL)
        {
            len+=1;
            tmp = tmp->next;
        }
        int m = len/k, n = len%k;
        int first = m + (n==0?0:1);
        vector<ListNode *> res(k, NULL);
        
        tmp = root;
        for(int i=1; i<=k; i++)
        {
            res[i-1] = tmp;
            int j=1;
            m = i<=n ? first : len/k;
            for(;j<m&&tmp&&tmp->next;j++)
            {
                tmp=tmp->next;
            }
            if(!tmp || !tmp->next)
                break;
            ListNode *t = tmp->next;
            tmp->next = NULL;
            tmp = t;
        }
        return res;
    }
};

7-6 链表去重 分数 10 作者 陈越 单位 浙江大学 给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉。即对每个键值 K,只有第一个绝对值等于 K 的结点被保留。同时,所有被删除的结点须被保存在另一个链表上。例如给定 L 为 21→-15→-15→-7→15,你需要输出去重后的链表 21→-15→-7,还有被删除的链表 -15→15。 输入格式: 输入在第一行给出 L 的第一个结点的地址和一个正整数 N(≤10 5 ,为结点总数)。一个结点的地址是非负的 5 位整数,空地址 NULL 用 -1 来表示。 随后 N 行,每行按以下格式描述一个结点: 地址 键值 下一个结点 其中地址是该结点的地址,键值是绝对值不超过10 4 的整数,下一个结点是下个结点的地址。 输出格式: 首先输出去重后的链表,然后输出被删除的链表。每个结点占一行,按输入的格式输出。 输入样例: 00100 5 99999 -7 87654 23854 -15 00000 87654 15 -1 00000 -15 99999 00100 21 23854 输出样例: 00100 21 23854 23854 -15 99999 99999 -7 -1 00000 -15 87654 87654 15 -1 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB Python (python3) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import sys #正确读取第一行:头节点地址和节点总数 first_line=sys.stdin.readline().strip().split() head=first_line[0] n=int(first_line[1]) #创建节点字典,存储所有节点信息 nodes={} for _ in range(n): parts=sys.stdin.readline().strip().split() addr,key,next_addr=parts[0],int(parts[1]),parts[2] nodes[addr]=(key,next_addr) #用于记录已出现的绝对值 seen=set() #去重后的链表和被删除的链表 result_head=None result_tail=None removed_head=None removed_tail=None current=head while current != '-1': key,next_addr=nodes[current] abs_key=abs(key) if abs_key not in seen: #首次出现,加入结果链表 seen.add(abs_key) if result_head is None: result_head=current result_tail=current else: #更新上一个节点的next指针 nodes[result_tail]=(nodes[result_tail][0],current) result_tail=current else: #已出现过,加入被删除链表 if removed_head is None: removed_head=current 用python解答
10-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值