Java for LeetCode 025 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 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

解题思路:

和k=2情况差不多,考虑到后进先出,可以用Stack实现,JAVA代码如下:

	public ListNode reverseKGroup(ListNode head, int k) {
		ListNode result = new ListNode(0), index = result,temp=head;
		if (head == null)
			return null;
		Stack<Integer> stack =new Stack<Integer>();
		while (temp!=null) {
			for (int i = 0; i < k; i++) {
				stack.push(temp.val);
				temp = temp.next;
				if (temp == null&&i<k-1) {
					index.next = head;
					return result.next;
				}
			}
			while(!stack.isEmpty()){
				index.next=new ListNode(stack.pop());
				index=index.next;
				head=head.next;
			}
		}
		return result.next;
	}
时间: 2024-08-29 06:41:20

Java for LeetCode 025 Reverse Nodes in k-Group的相关文章

[LeetCode] 025. Reverse Nodes in k-Group (Hard) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 025. Reverse Nodes in k-Group (Hard) 链接: 题目:https://oj.leetcode.com/problems/reverse-nodes-in-k-group/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个链表每

LeetCode 025 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

Java for LeetCode 092 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 ≤ le

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

LeetCode:Reverse Nodes in k-Group

1.题目名称 Reverse Nodes in k-Group(分组翻转链表) 2.题目地址 https://leetcode.com/problems/reverse-nodes-in-k-group 3.题目内容 英文: 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 multipl

[LeetCode 题解]: Reverse Nodes in K-Groups

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 Nodes in k-groups

问题: 给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置.少于k个的部分不做处理. 分析: 个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法.其他的考察点如果还有的话,就的细心程度. 实现: void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next =

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)

Problem: 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,