`
hcx2013
  • 浏览: 88792 次
社区版块
存档分类
最新评论

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

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (k < 2) {
        	return head;
        }
        ListNode cur = head;
        ListNode start = null;
        ListNode pre = null;
        ListNode next = null;
        int cnt = 1;
        while (cur != null) {
        	next = cur.next;
        	if (cnt == k) {
        		start = pre == null ? head : pre.next;
        		head = pre == null ? cur : head;
        		resign(pre, start, cur, next);
        		pre = start;
        		cnt = 0;
        	}
        	cnt++;
        	cur = next;
        }
        return head;
    }

	private void resign(ListNode left, ListNode start, ListNode end,
			ListNode right) {
		// TODO Auto-generated method stub
		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;
	}
}

 

 

0
5
分享到:
评论

相关推荐

    js-leetcode题解之25-reverse-nodes-in-k-group.js

    js js_leetcode题解之25-reverse-nodes-in-k-group.js

    c语言-leetcode 0025-reverse-nodes-in-k-group.zip

    c c语言_leetcode 0025_reverse_nodes_in_k_group.zip

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer ...25. Reverse Nodes in k-Group 26. Remove Dupli

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest ...Reverse ...Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3

    算法面试通关40讲完整课件 05-07 数组、链表

    5. K 个一组翻转链表(Reverse Nodes in K-Group, 如LeetCode的第25题):将链表中的节点每K个一组进行反转。 解决这些题目时,通常需要掌握以下技巧: - 使用快慢指针(Floyd's Tortoise and Hare)来检测链表环。...

    LeetCode最全代码

    421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...

    leetcode-cpp刷题

    - **2.2.9 Reverse Nodes in k-Group** - 每k个一组反转链表。 - 实现思路:维护一个指针指向当前组的前一个节点,依次反转每一组。 - **2.2.10 Copy List with Random Pointer** - 复制一个带随机指针的链表。...

    _leetcode-python.pdf

    - Swap Nodes in Pairs / Reverse Nodes in k-Group: 这两个问题涉及到在链表中按特定规则交换节点或反转节点组。 - Remove Duplicates from Sorted Array / Remove Element: 删除排序数组中的重复项,或从数组中...

    多线程leetcode-leetcode-java:leetcode上的题解,基于java语言

    多线程 leetcode 前言 每天刷点leetcode,基于java语言实现。 leetcode上难度分三档:easy,medium,hard. 如下: easy medium Remove Nth ...Nodes in ...k ...Reverse Nodes in k-Group Trapping Rain Water

    algorithm-homework

    K 个一组翻转链表, reverse-nodes-in-k-group.ts 旋转数组, rotate-array.ts 滑动窗口最大值, sliding-window-maximum.ts 两两交换链表中的节点, swap-nodes-in-pairs.ts 三数之和, three-sum.ts 接雨水, trapping-...

    leetcode296-leetcode-in-py-and-go:Go中的Leetcode

    reverse-nodes-in-k-group: 解析 pre_for_next 到辅助函数 29:除以两个整数:溢出; 两反 31:下一个排列:再做一次(排序!) 32:最长有效(),使用栈,左推idx 33: search-in-rotated-sorted-array ,比较中间值...

    leetcode添加元素使和等于-leetcode:力码

    leetcode添加元素使和等于 总结 按照类别分类来刷 刷当前题的时候,看下『题目描述』最底下有个『相似题目』,这些题的思路大致也相当 ...reverse-nodes-in-k-group 二叉树 实现一个二叉树 二叉树二叉树的

    Dir-For-LeetCode

    问题 ... 025_Reverse_Nodes_in_k-Group 026_Remove_Duplicates_from_Sorted_Array 027_Remove_Element 028_Implement_strStr() 029_Divide_Two_Integers 030_Substring_with_Concatenation_of

    VCS installation

    - A cluster refers to a group of interconnected computers working together to provide a unified system image. Clusters can be used for various purposes, including high availability and load balancing....

    Amazon近半年电面题.pdf

    20. SwapNodesinPairs: 在链表中交换每两个相邻节点,这需要处理好链表的指针。 21. ReverseNodesink-Group: 以k个节点为一组翻转链表,这是一个在链表上的递归或迭代问题。 22. RemoveElement: 从数组中移除特定...

Global site tag (gtag.js) - Google Analytics