`
cozilla
  • 浏览: 92106 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[Leetcode] Reverse Nodes in k-Group

 
阅读更多
Reverse Nodes in k-GroupFeb 16 '123953 / 13303

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.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        if (k == 1) return head;

        ListNode *start, *end, *prev, *next, *my_head;
        start = end = head;
        my_head = new ListNode(123);
        my_head->next = head;
        prev = my_head;

        while (start != NULL) {
            int t = 1;
	        while (end != NULL && t < k) {
	        	t++;
	        	end = end->next;
	        }
	        if (t == k && end != NULL) {
	        	next = end->next;
	        	reverse(start, end, prev, next);
	  	        prev = start; 
	        	start = end = prev->next;
	        } else {
                break;   
	        }
        }

        ListNode* res = my_head->next;
        delete my_head;
        return res;
    }

    void reverse(ListNode* s, ListNode* e, ListNode* p, ListNode* n) {
    	
    	ListNode *cur = s, *prev = NULL, *next;
    	while (cur != n) {
    		next = cur->next;
    		cur->next = prev;
    		prev = cur;
            cur = next;
    	}
    	p->next = e;
    	s->next = n;
    }
};	

 

 

@2013-10-03

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        if (head == NULL) return head;
        ListNode *s, *e, *next, *prev, myhead(99);
        myhead.next = head;
        prev = &myhead;
        s = head;
        int cnt;
        while (s != NULL) {
            cnt = 1;
            e = s;
            while (e != NULL && cnt < k) e = e->next, cnt++;
            if (cnt < k || e == NULL) break;
            next = e->next;
            reverse(s, e);
            prev->next = e;
            s->next = next;
            prev = s;
            s = next;
        }
        return myhead.next;
    }
    
    void reverse(ListNode* head, ListNode* tail) {
        ListNode* prev = NULL, *cur = head, *next;
        while (prev != tail) {
            next = cur->next;
            cur->next = prev;
            prev = cur;
            cur = next;
        }
        
    }
};

 

分享到:
评论

相关推荐

    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

    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 ,比较中间值...

    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

    多线程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

    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添加元素使和等于-leetcode:力码

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

    _leetcode-python.pdf

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

    leetcode-cpp刷题

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

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

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

    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

    Amazon近半年电面题.pdf

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

Global site tag (gtag.js) - Google Analytics