C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转)
来源:互联网 发布:织梦cms怎么安装 编辑:程序博客网 时间:2024/04/29 22:30
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.
双指针,没什么难点。
//// 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;}voidtest_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);}/********************************************************************//********************************************************************/
0 0
- C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转)
- C实现 LeetCode->Partition List(双指针大法)(单链表)
- C实现 LeetCode->Reverse Linked List II (双指针大法)(单链表反转)
- C实现 LeetCode->Reverse Linked List (双指针大法)(单链表反转)
- C实现 LeetCode->Linked List Cycle 双指针大法)(单链表是否有环)
- C实现 LeetCode->Reorder List (双指针大法)(单链表是否有环)
- C实现 LeetCode->Linked List Cycle 双指针大法)(单链表是否有环 并计算环长度)
- C实现 LeetCode->Merge Two Sorted Lists (双指针大法) (单链表排序)
- C实现 LeetCode->Merge Two Sorted Lists (双指针大法)(单链表swap合并反转)
- C实现 LeetCode->Reverse Nodes in k-Group (双指针大法)(单链表反转)
- leetcode Rotate list (链表旋转)的python实现
- 【LeetCode-面试算法经典-Java实现】【061-Rotate List(旋转单链表)】
- C实现 LeetCode->Merge k Sorted Lists (双指针大法)
- C实现 LeetCode->Merge Two Sorted Lists (双指针大法)
- LeetCode 之 Rotate List — C 实现
- Rotate List 部分旋转链表
- LeetCode OJ 之 Rotate List (链表旋转)
- LeetCode 61. Rotate List(旋转链表)
- CoreMotion框架-iOS设备的核心运动
- 个人笔记:正则表达式 001
- android BitMap回收
- 开始刷leetcode day47:Gray Code
- Python编码规范
- C实现 LeetCode->Rotate List(双指针大法)(单链表 部分旋转)
- 小数点自动进位
- grails配置tomcat-->server.xml Context>path>docBase
- null对象可以访问它所属类的类成员(即static修饰的成员),但是null对象访问实例成员将会引发错误。
- mac下反编译apk
- c#字符串格式化
- grails默认全局gorm配置
- c++中的explicit
- 关于MKMapView -地图展示