C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转)
2015-06-22 11:38
621 查看
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.
双指针,没什么难点。
For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
双指针,没什么难点。
// // 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); } /********************************************************************/ /********************************************************************/
相关文章推荐
- android BitMap回收
- 个人笔记:正则表达式 001
- Windows10-加速在企业中的部署
- Windows10-加速在企业中的部署
- 小数点自动进位
- CoreMotion框架-iOS设备的核心运动
- 开始刷leetcode day47:Gray Code
- Android开发之Handler(五)几种常见的传值方式
- grails、groovy纯sql语句是正常但还是提示:无效的关系运算符
- 【20150622】杂项整理
- Redis 笔记与总结3 list 类型
- Android开发之handler(四)
- Android:Json解析库的选择
- Android开发之handler(三)handler真的是重新启动一个线程吗?
- grails查询数据库最大id
- grils通过sql查出来的字段第一个字母转换成大写:
- 阿里都上市了,这些牛企为何誓死不上市?
- Linux-shell基础-3
- poj 1083 Moving Tables
- C++ 标准库中的异常 http://blog.csdn.net/hzhxxx/article/details/7616336