C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转)


Given a list, rotate the list to the right by k places, where k is non-negative.

 For example:

 Given 1->2->3->4->5->NULL and k = 2,

 return 4->5->1->2->3->NULL.

 

 

 双指针,没什么难点。

C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转)_第1张图片
//
//  RotateList.c
//  Algorithms
//
//  Created by TTc on 15/6/22.
//  Copyright (c) 2015年 TTc. All rights reserved.
//
/**
 *  Given a list, rotate the list to the right by k places, where k is non-negative.
 For example:
 Given 1->2->3->4->5->NULL and k = 2,
 return 4->5->1->2->3->NULL.
 
 
 双指针,没什么难点。
 */
#include "RotateList.h"
#include <stdlib.h>
#include <string.h>
#include "List.h"


/********************************************************************/
// LeetCode 答案
/********************************************************************/
struct ListNode {
    int val;
    struct ListNode *next;
};


struct ListNode*
rotateRight(struct ListNode* head, int k) {
    
    if(head == NULL || head->next == NULL) return head;
    
    struct ListNode *Tracker = head;
    struct ListNode *Looper = head;
    
    int size = 0;
    while(Looper != NULL){
        Looper = Looper->next;
        size++;
    }
    k = k % size;
    Looper = head;
    
    /*主循环*/
    int count = 0;
    while(Looper->next != NULL){
        if(count >= k) Tracker = Tracker->next;
        count++;
        Looper = Looper->next;
    }
    
    /*重构单链表*/
    Looper->next = head;
    head = Tracker->next;
    Tracker->next = NULL;
    
    return head;
}


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


/********************************************************************/
// List.c 是范性类 单链表
/********************************************************************/

ListElmt*
tt_rotateRight(ListElmt* head, int k) {
    
    if(head == NULL || head->next == NULL) return head;
    
    ListElmt *Tracker = head;
    ListElmt *Looper = head;
    
    int size = 0;
    while(Looper != NULL){
        Looper = Looper->next;
        size++;
    }
    k = k % size;
    Looper = head;
    
    /*主循环*/
    int count = 0;
    while(Looper->next != NULL){
        if(count >= k) Tracker = Tracker->next;
        count++;
        Looper = Looper->next;
    }
    
    /*重构单链表*/
    Looper->next = head;
    head = Tracker->next;
    Tracker->next = NULL;
    
    return head;
}


void
test_tt_rotateRight(){
    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_rotateRight(list_head(&l1),3);
    printf("result->val===%d\n",*(int *)result->data);
    print_listNode(result);
}


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




你可能感兴趣的:(C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转))