链表反转的几种形式
来源:互联网 发布:知乎 感情 精华贴 编辑:程序博客网 时间:2024/06/05 15:51
// From curr reverse to end, return reversed linked list head.ListNode *unguarded_reverse(ListNode *prev, ListNode *curr, ListNode *end){ ListNode *next = NULL; while (curr != end) { next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev;} // Reverse linked list from m to n, return head.// For example:// Given 1->2->3->4->5->NULL, m = 2 and n = 4,// return 1->4->3->2->5->NULL.ListNode *reverse_between(ListNode *head, int m, int n) { if (head == NULL || m <= 0 || n <= 0) { return NULL; } if (m >= n) return head; ListNode **backup = &head, *p = head, *q = head; // Move p to the mth node, q to the nth node. while (p != NULL && --m) backup = &p->next, p = p->next; while (q != NULL && --n) q = q->next; // If move success. if (m == 0 && n == 0 && p != NULL && q != NULL) { *backup = unguarded_reverse(q->next, p, q->next); } return head;} // Reverse every k group in linked list, return head.// 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->5ListNode *reverse_k_group(ListNode *head, int k) { if (head == NULL || k < 0) return NULL; int count = 0; // Count all node num. for (ListNode *p = head; p != NULL; p = p->next) { ++count; } for (int i = 1; i + k - 1 <= count; i += k) { head = reverse_between(head, i, i + k - 1); } return head;}
0 0
- 链表反转的几种形式
- 链表反转的几种方法
- 反转链表的几种方法
- 反转单向链表的几种方法
- DDOS 的几种形式
- 封装的几种形式
- 数据传输的几种形式
- ListView的几种形式
- ListView的几种形式
- pooling的几种形式
- 关于mysql锁表的几种形式
- 关于mysql锁表的几种形式
- 字符串的几种常见的反转
- 几种编码的形式的介绍
- 几种编码的形式的介绍
- 字符串反转的几种算法
- Python字符串反转的几种方法
- 字符串反转的几种方法
- Scala: Under The Hood of Hello World
- 活了20年多了才知道!蜂蜜十大鲜见真相吓掉小命
- Android 动画杂谈 (三)
- 单源最短路径
- UI-之plist文件解析
- 链表反转的几种形式
- HDU 5583 Kingdom of Black and White(依旧是暴力)——2015ACM/ICPC亚洲区上海站
- 软件测试基础题
- 《如何高效学习》读书笔记
- Oracle程序包
- 用Block 进行pop和传值
- Android开源BezierView:仿QQ未读消息99+条的红色气泡
- 网页设计心得报告(1)by ksy
- Mac系统 unity VScode 开发