LeetCode 25 Reverse Nodes in k-Group 代码

dr-von.top

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.

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

Note:

Only constant extra memory is allowed.
You may not alter the values in the list’s nodes, only nodes itself may be changed.

题意不难:每k个节点为一组,每组都进行链表反转。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        chead = head
        # 分组
        for i in range(k):
            if not chead:
                return head
            chead = chead.next
        # 分组后的首节点作为合并后的尾节点
        last_node = self.reverseKGroup(chead, k)
        # 组内进行链表的反转
        chead = head
        for i in range(k):
            cashnext = chead.next
            chead.next = last_node
            last_node = chead
            chead = cashnext
        return last_node
        
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这是一个用C语言实现的LeetCode题目:Group Anagrams。 题目描述: 给定一个字符串数组,将其分组,使得包含相同字符的字符串在同一组中。返回结果应按照字母顺序排列。 示例: 输入: ["eat", "tea", "tan", "ate", "nat", "bat"] 输出: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] 解题思路: - 遍历字符串数组,将每个字符串转换为字符数组并排序。 - 使用哈希表将排序后相同的字符串分组。 - 遍历哈希表,将每个分组的字符串数组添加到结果数组中。 代码实现: /** * Note: The returned array must be malloced, assume caller calls free(). */ char *** groupAnagrams(char ** strs, int strsSize, int* returnSize, int** returnColumnSizes){ char ***res = (char ***)malloc(sizeof(char **) * strsSize); *returnSize = 0; *returnColumnSizes = (int *)malloc(sizeof(int) * strsSize); int cnt[26] = {0}; int index = 0; struct Node *hashTable[strsSize]; memset(hashTable, 0, sizeof(hashTable)); for (int i = 0; i < strsSize; i++) { memset(cnt, 0, sizeof(cnt)); for (int j = 0; j < strlen(strs[i]); j++) { cnt[strs[i][j] - 'a']++; } char *key = (char *)malloc(sizeof(char) * 27); int idx = 0; for (int j = 0; j < 26; j++) { if (cnt[j] != 0) { key[idx++] = j + 'a'; key[idx++] = cnt[j] + '0'; } } key[idx] = '\0'; int hash = getHash(key); if (hashTable[hash] == NULL) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->key = key; node->next = NULL; node->strings = (char **)malloc(sizeof(char *)); node->strings[0] = strs[i]; node->count = 1; hashTable[hash] = node; } else { struct Node *p = hashTable[hash]; while (p != NULL) { if (strcmp(p->key, key) == 0) { p->count++; p->strings = (char **)realloc(p->strings, sizeof(char *) * p->count); p->strings[p->count - 1] = strs[i]; break; } p = p->next; } if (p == NULL) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->key = key; node->next = hashTable[hash]; node->strings = (char **)malloc(sizeof(char *)); node->strings[0] = strs[i]; node->count = 1; hashTable[hash] = node; } } } for (int i = 0; i < strsSize; i++) { if (hashTable[i] != NULL) { struct Node *p = hashTable[i]; while (p != NULL) { res[index] = p->strings; (*returnColumnSizes)[index] = p->count; (*returnSize)++; p = p->next; index++; } } } return res; } int getHash(char *key) { int hash = 0; for (int i = 0; i < strlen(key); i++) { hash = (hash * 31 + key[i]) % 100000; } return hash; } struct Node { char *key; struct Node *next; char **strings; int count; };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值