LeetCode 25:Reverse Nodes in k-Group

来源:互联网 发布:波士顿矩阵分析法应用 编辑:程序博客网 时间:2024/05/16 08:55

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 = 3, you should return: 3->2->1->4->5

Subscribe to see which companies asked this question

方法一:利用栈结构

//利用一个栈来实现每k个结点的逆序,//每组逆序结束后,将每组最后一个结点与下一组第一个结点重新链接//例如:对单链表1->2->3->4->5->6->7->8->9进行每4个结点一组进行逆序class Solution{public:ListNode* reverseKGroup(ListNode* head, int k) {if (head == NULL || head->next == NULL || k < 2)return head;stack<ListNode*> stack1;ListNode *newHead = head;ListNode *cur = head;ListNode *pre = NULL;ListNode *next = NULL;while (cur != NULL){next = cur->next;stack1.push(cur);   //将结点依次入栈if (stack1.size() == k)  //当入栈结点达到k个时,这k个结点开始逆序{pre = resign1(stack1, pre, next);  //pre=cur1 第一组结点逆序后,pre指向结点1while (!stack1.empty())    //每组逆序后,将stack清空{stack1.pop();}newHead = newHead == head ? cur : newHead;  //记录新的头结点,第一组全部入栈后,cur指向结点4,                                            //第一组结点逆序后,新头结点变为4}cur = next;   //第一组逆序结束后,cur重新指向下一组的第一个结点5,开始遍历下一组}return newHead;}//将k个结点按照出栈顺序依次重新链接(比如:原单链表1->2->3->4,出栈后变成4->3->2->1)ListNode * resign1(stack<ListNode*>stack2, ListNode* left, ListNode* right){ListNode *cur1 = stack2.top();if (left != NULL){left->next = cur1;}ListNode *next = NULL;while (!stack2.empty())  {next = stack2.top( );cur1->next = next;     //依次重新链接cur1 = next;stack2.pop();}cur1->next = right;  //1->2->3->4,出栈后变成4->3->2->1后,cur1指向结点1,重新链接上结点5                     //1->2->3->4->5->6->7->8->9变为4->3->2->1->5->6->7->8->9return cur1;}};


完整测试代码:

#include <iostream>#include<stack>#include<list>using namespace std;struct ListNode{int data;ListNode *next;ListNode(int x) :data(x), next(NULL){}};//利用一个栈来实现每k个结点的逆序,//每组逆序结束后,将每组最后一个结点与下一组第一个结点重新链接class Solution{public:ListNode* reverseKGroup(ListNode* head, int k) {if (head == NULL || head->next == NULL || k < 2)return head;stack<ListNode*> stack1;ListNode *newHead = head;ListNode *cur = head;ListNode *pre = NULL;ListNode *next = NULL;while (cur != NULL){next = cur->next;stack1.push(cur);   //将结点依次入栈if (stack1.size() == k)  //当入栈结点达到k个时,这k个结点开始逆序{pre = resign1(stack1, pre, next);  //pre=cur1 第一组结点逆序后,pre指向结点1while (!stack1.empty())    //每组逆序后,将stack清空{stack1.pop();}newHead = newHead == head ? cur : newHead;  //记录新的头结点,第一组全部入栈后,cur指向结点4,                                                                                  //第一组结点逆序后,新头结点变为4}cur = next;   //第一组逆序结束后,cur重新指向下一组的第一个结点5,开始遍历下一组}return newHead;}//将k个结点按照出栈顺序依次重新链接(比如:原单链表1->2->3->4,出栈后变成4->3->2->1)ListNode * resign1(stack<ListNode*>stack2, ListNode* left, ListNode* right){ListNode *cur1 = stack2.top();if (left != NULL){left->next = cur1;}ListNode *next = NULL;while (!stack2.empty())  {next = stack2.top( );cur1->next = next;     //依次重新链接cur1 = next;stack2.pop();}cur1->next = right;  //1->2->3->4,出栈后变成4->3->2->1后,cur1指向结点1,重新链接上结点5                                //1->2->3->4->5->6->7->8->9变为4->3->2->1->5->6->7->8->9return cur1;}};//创建只有空链表,返回一个dummy节点ListNode * createList(){ListNode *dummy = new ListNode(-1);return dummy;}ListNode * initList(ListNode *dummy){//用一个数组初始化链表int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };ListNode *tmp, *head;tmp = dummy;//使用的是尾插入法for (int i = 0; i<sizeof(array) / sizeof(int); i++){ListNode *p = new ListNode(array[i]);tmp->next = p;tmp = tmp->next;}head = dummy->next;delete dummy;return head;}//显示链表void showList(ListNode *head){while (head){printf("%d ", head->data);head = head->next;}printf("\n");return;}//摧毁链表void desroyList(ListNode *head){while (head){ListNode *tmp = head->next;delete head;head = tmp;}return;}void main(){Solution sol1;//创建一个空链表ListNode *dummy = createList();//对链表初始化,返回headListNode *head = initList(dummy);//显示链表showList(head);//swaphead = sol1.reverseKGroup(head, 4);//显示链表showList(head);system("pause");desroyList(head);}

方法二:不利用栈结构,直接在原链表中调整

//不利用栈结构,在原链表中直接调整class Solution{public:ListNode* reverseKGroup(ListNode* head, int k) {if (head == NULL || head->next == NULL || k < 2)return head;ListNode *cur = head;ListNode *start = NULL;ListNode *pre = NULL;ListNode *next = NULL;int count = 1;while (cur != NULL){next = cur->next;if (count == k)  {start = pre == NULL ? head : pre->next;head = pre == NULL ? cur : head;resign2(pre, start, cur, next);pre = start;count = 0;}count++;cur = next;}return head;}void resign2(ListNode* left, ListNode* start, ListNode *end,ListNode* right){ListNode *pre = start;ListNode* cur = start->next;ListNode*next = NULL;while (cur!=right)  {next = cur->next;cur->next = pre;pre = cur;cur = next;}if (left != NULL){left->next = end;}start->next = right; //上一组的最后一个结点(逆序前的第一个结点)链接下一组逆序前的第一个结点}};




1 0
原创粉丝点击