标签:面试题 algorithm 算法 leetcode 递归
给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置。少于k个的部分不做处理。
void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next = pre->next; pre->next = head; if(tail == NULL) tail = head; head = next; } pre = tail; }
void hh(ListNode*& pre, ListNode*head, int k){ if(head == NULL || k <= 1) { pre->next = head; return; } int dis = 1; ListNode* tail = head; while (dis < k && tail) { ++dis; tail = tail->next; } if(tail == NULL) { pre->next = head; return; } ListNode *newhead = tail->next; tail->next = NULL; reverseList(pre, head); hh(pre, newhead, k); }
ListNode *reverseKGroup(ListNode *head, int k) { ListNode* newHead = new ListNode(-1); ListNode* pre = newHead; hh(pre, head, k); return newHead->next; }
ListNode *reverseKGroup(ListNode *head, int k) { if (head == NULL || head->next == NULL || k == 1) return head; ListNode *new_head = new ListNode(-1); new_head->next = head; int visited_len = 0; ListNode *pre, *tail, *travel; pre = new_head; while (1) { travel = pre; visited_len = 0; while (travel != NULL && visited_len < k) { travel = travel->next; if(travel) visited_len++; } if(visited_len < k) break; travel = travel->next;//the next begin position. ListNode *p = tail = pre->next; pre->next = travel; ListNode *q = p->next; do{ p->next = pre->next; pre->next = p; p = q; if(p) q = p->next; }while(p != travel); pre = tail; } pre = new_head; new_head = new_head->next; delete pre; return new_head; }
【leetcode】reverse Nodes in k-groups,布布扣,bubuko.com
【leetcode】reverse Nodes in k-groups
标签:面试题 algorithm 算法 leetcode 递归
原文地址:http://blog.csdn.net/shiquxinkong/article/details/29849573