[LeetCode] Reverse Nodes in k-Group
来源:互联网 发布:照片分析软件 编辑:程序博客网 时间:2024/05/19 00:40
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 = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
相关问题:Swap Nodes in Pairs
题目的要求是,当链表剩余长度不足k,不进行翻转。下面给出一个算法,该算法会将剩余链表翻转,不论剩余链表的长度是否小于k。
ListNode *reverseKGroup(ListNode *head, int k) { ListNode* current = head; ListNode* next = NULL; ListNode* prev = NULL; int count = 0; /*reverse first k nodes of the linked list */ while (current != NULL && count < k) { next = current->next; current->next = prev; prev = current; current = next; count++; } /* next is now a pointer to (k+1)th node Recursively call for the list starting from current. And make rest of the list as next of first node */ if(next != NULL) { head->next = reverseKGroup(next, k); } /* prev is new head of the input list */ return prev; }
下面的代码,不会反转剩余链表。与上面代码唯一不同之处在于,多了一个 if(count<k) 的判断。
ListNode *reverseKGroup(ListNode *head, int k) { ListNode* current = head; ListNode* next = NULL; ListNode* prev = NULL; int count = 0; /*reverse first k nodes of the linked list */ while (current != NULL && count < k) { next = current->next; current->next = prev; prev = current; current = next; count++; } // The following code block will reverse the tail sub-list whose size is less than k if(count<k) { current = prev; next = NULL; prev = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } return head; } /* next is now a pointer to (k+1)th node Recursively call for the list starting from current. And make rest of the list as next of first node */ if(next != NULL) { head->next = reverseKGroup(next, k); } /* prev is new head of the input list */ return prev; }
0 0
- LeetCode:Reverse Nodes in k-Group
- LeetCode: Reverse Nodes in k-Group
- LeetCode : Reverse Nodes in k-Group
- leetcode 42: Reverse Nodes in k-Group
- [Leetcode] reverse nodes in K-Group
- [LeetCode]Reverse Nodes in k-Group
- LeetCode-Reverse Nodes in k-Group
- LeetCode - Reverse Nodes in k-Group
- LeetCode | Reverse Nodes in k-Group
- 【leetcode】Reverse Nodes in k-Group
- Leetcode: Reverse Nodes in k-Group
- leetcode Reverse Nodes in k-Group
- [LeetCode] Reverse Nodes in k-Group
- LeetCode题解: Reverse Nodes in k-Group
- 【Leetcode】Reverse Nodes in k-Group
- LeetCode Reverse Nodes in k-Group
- [Leetcode] Reverse Nodes in k-Group (Java)
- LeetCode(25)Reverse Nodes in K-Group
- Java final介绍
- 移动开发过程概览
- DBMS_SCHEDULER执行PERL脚本加载数据
- InnoDB与Myisam的六大区别
- PostgreSQL PITR示例 (物理备份+WAL日志)
- [LeetCode] Reverse Nodes in k-Group
- create table like 和create table select 比较
- CF 题目小记sel
- 学习Python大数据处理模块Pandas
- 自适应网页设计的方法
- IOS客户端公共WIFI解决方案
- 知识点点:(C#)字符串类型操作——string
- XML入门问答
- BW