C实现 LeetCode->Reverse Nodes in k-Group (双指针大法)(单链表反转)



Given a linked list, reverse the nodes of a linked list k at a time and return its modified 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


 反转单链表的基础题。


 1:不用额外空间

 2:不改动结点值,很容易写错

 3:建议把Reverse()这个函数背下来。





//
//  ReverseNodesInk-Group.c
//  Algorithms
//
//  Created by TTc on 15/6/19.
//  Copyright (c) 2015年 TTc. All rights reserved.
//
/**
 *  Given a linked list, reverse the nodes of a linked list k at a time and return its modified 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
 
 反转链表的基础题。
 
 思路很清楚,但是在不用额外空间和不改动结点值这两个条件下,很容易写错。建议把Reverse()这个函数背下来。
 */
#include "ReverseNodesInk-Group.h"
#include <stdlib.h>
#include <string.h>
#include "List.h"
/********************************************************************/
// LeetCode 答案
/********************************************************************/
struct ListNode {
    int val;
    struct ListNode *next;
};

//双指针大法
/*Since only constant memory is allowed, time cost is O(k) */
static struct ListNode*
Reverse(struct ListNode *begin, struct ListNode *end){
   struct ListNode* prev = begin->next;
   struct ListNode* curr = prev->next;
    
    while(curr != end){
        prev->next = curr->next;
        curr->next = begin->next;
        begin->next = curr;
        curr = prev->next;
    }
    return prev;
}

struct ListNode*
reverseKGroup(struct ListNode* head, int k) {
    if(head == NULL || head->next == NULL || k == 1) return head;
    
    struct ListNode *dummyHead = (struct ListNode *)malloc(sizeof(*dummyHead));
    dummyHead->next = head;
    
    struct ListNode *prev = head;
    struct ListNode *curr = dummyHead;
    int count = 0;
    
    while(prev != NULL){
        count++;
        if(count == k){
            curr = Reverse(curr, prev->next);
            prev = curr->next;
            count = 0;
        }
        else prev = prev->next;
    }
    
    return dummyHead->next;
}

/********************************************************************/
/********************************************************************/





/********************************************************************/
// List.c 是范性类 单链表
/********************************************************************/
//反转单链表中  begin  到 end 节点
static ListElmt*
tt_Reverse(ListElmt *begin, ListElmt *end){
    ListElmt* prev = begin->next;
    ListElmt* curr = prev->next;
    
    while(curr != end){
        prev->next = curr->next;
        curr->next = begin->next;
        begin->next = curr;
        curr = prev->next;
    }
    return prev;
}
//反转 长度为k的 每组 节点
ListElmt*
tt_reverseKGroup(ListElmt * head, int k) {
    if(head == NULL || head->next == NULL || k == 1) return head;
    
    ListElmt *dummyHead = (ListElmt *)malloc(sizeof(*dummyHead));
    dummyHead->next = head;
    
    ListElmt *prev = head;
    ListElmt *curr = dummyHead;
    int count = 0;
    
    while(prev != NULL){
        count++;
        if(count == k){
            curr = tt_Reverse(curr, prev->next);
            prev = curr->next;
            count = 0;
        }
        else prev = prev->next;
    }
    
    return dummyHead->next;
}


void
test_tt_reverseKGroup(){
    List l1;
    list_init(&l1, free);
    int *data ;
    int array[15] = {100,200,300,4000,5000,600,700,800,900,100000};
    for (int i = 0; i< 10; i++) {
        if ((data = (int *)malloc(sizeof(int))) == NULL)
            return ;
        *data = array[i];
        if (list_ins_next(&l1, NULL, data) != 0)  //逐个插入元素
            return;
    }
    print_list(&l1);
    
    ListElmt *result = tt_reverseKGroup(list_head(&l1),3);
    printf("result->val===%d\n",*(int *)result->data);
    
    print_listNode(result);

}

/********************************************************************/
/********************************************************************/







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值