关闭

【Leetcode】Reverse Nodes in k-Group

371人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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

思路:

利用 Reverse Linked List II 中的算法,可以方便的做出来,不过时间复杂度是O(n^2)。更好的解法2 是通过本次反转起始指针往下移k,得到下次反转指针,算法复杂度为O(n)。有空会把解法2实现一下。。= =

算法:

	public ListNode reverseKGroup(ListNode head, int k) {
		int length = getListLength(head);
		for (int i = 1; (i + k - 1) <= length; i += k) {
			head = reverseBetween(head, i, i + k - 1);
		}
		return head;
	}

	public int getListLength(ListNode head) {
		int length = 0;
		ListNode p = head;
		while (p != null) {
			length++;
			p = p.next;
		}
		return length;
	}

	public ListNode reverseBetween(ListNode head, int m, int n) {
		if (head == null)
			return null;
		ListNode newHead = new ListNode(0);
		newHead.next = head;
		// start/head~rear/end,反转head到rear之间的结点,start/end是反转区间两个结点
		ListNode rear = head, start = newHead, end = head.next;
		m = m - 1;
		while (m-- > 0)// 因为start是从头结点开始算,所以要多走一步,即m--,而不是像下面的--n
			start = start.next;
		head = start.next;
		while (--n > 0)
			rear = rear.next;
		end = rear.next;
		// ===反转 head到rear之间结点
		ListNode p = head, q, t;
		q = p.next;
		while (p != rear) {
			t = q.next;
			q.next = p;
			p = q;
			q = t;
		}
		// ===
		start.next = rear;
		head.next = end;
		return newHead.next;
	}


0
0
查看评论

(Java)LeetCode-25. 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 multipl...
  • u012848330
  • u012848330
  • 2016-06-02 22:06
  • 509

【Leetcode】【python】Reverse Nodes in k-Group

题目大意将一个链表中每k个数进行翻转,末尾不足k个的数不做变化。解题思路参考: https://shenjie1993.gitbooks.io/leetcode-python/025%20Reverse%20Nodes%20in%20k-Group.html A->B->C->...
  • qqxx6661
  • qqxx6661
  • 2017-09-05 09:00
  • 112

[LeetCode]Reverse Nodes in k-Group, 解题报告

前言 今天收到了阿里的电子入职offer,看岗位的要求是要熟悉linux内核,迅速入手了一本《Linux内核设计与实现》,入职前希望能翻完一遍,在亚马逊货到付款下周二才能送到,这两天还是按部就班的写写LeetCode代码 题目 Given a linked list, reve...
  • zinss26914
  • zinss26914
  • 2014-02-08 15:51
  • 1712

[leetcode] 25. Reverse Nodes in k-Group 解题报告

题目链接:https://leetcode.com/problems/reverse-nodes-in-k-group/ Given a linked list, reverse the nodes of a linked list k at a time and retur...
  • qq508618087
  • qq508618087
  • 2016-01-08 10:51
  • 434

python写算法题:leetcode: 25. Reverse Nodes in k-Group

https://leetcode.com/problems/reverse-nodes-in-k-group/#/description class Solution(object): def reverseNode(self, nodes, k): for ind i...
  • lzf_china
  • lzf_china
  • 2017-06-29 21:29
  • 86

LeetCode解题报告--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...
  • github_27609763
  • github_27609763
  • 2015-10-17 23:48
  • 575

Reverse Nodes in k-Group -- LeetCode

原题链接: http://oj.leetcode.com/problems/reverse-nodes-in-k-group/  这道题是Swap Nodes in Pairs的扩展,Swap Nodes in Pairs其实是这道题k=2的特殊情况,大家可以先练习一下。不过...
  • linhuanmars
  • linhuanmars
  • 2014-02-26 06:27
  • 11216

LeetCode 25: 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 ...
  • sunao2002002
  • sunao2002002
  • 2015-06-08 20:00
  • 3674

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

[LeetCode] 025. Reverse Nodes in k-Group (Hard) (C++/Java)
  • hcbbt
  • hcbbt
  • 2015-03-05 15:04
  • 2749

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...
  • runningtortoises
  • runningtortoises
  • 2015-05-11 19:55
  • 895
    个人资料
    • 访问:568861次
    • 积分:8674
    • 等级:
    • 排名:第2678名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论