【leetcode】reverse Nodes in k-groups

问题:

给定一个链表的头指针,以及一个整数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;
}

上面所示函数,实现将head指示的链表进行逆置链接在pre的后面。

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);
}

上面的函数,对给定的head 指示的链表,找出首个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

时间: 2024-10-07 08:19:03

【leetcode】reverse Nodes in k-groups的相关文章

【leetcode】Reverse Nodes in k-Group

Reverse Nodes in k-Group 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 valu

【leetcode】Reverse Nodes in k-Group (hard)☆

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 nod

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

【LeetCode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【LeetCode】Reverse Linked List II 解题报告

[题目] Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n

【LeetCode】Swap Nodes in Pairs

题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list,

【LeetCode】Swap Nodes in Pairs (3 solutions)

Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the val