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

解答:

1. 用递归实现,逐层进行反转。遇到最后一个如果个数不为k,再反转一次即可。

2. 使用一个专用的反转函数来进行反转,从头到尾遍历,遍历到K的时候,使用Pre-Next指针的方式进行反转。这个方法比递归更棒。

 1 /*
 2      * Solution 2:
 3      * 使用区间反转的办法, iteration.
 4      * */
 5     public ListNode reverseKGroup(ListNode head, int k) {
 6         if (head == null) {
 7             return null;
 8         }
 9
10         ListNode dummy = new ListNode(0);
11         dummy.next = head;
12
13         ListNode cur = head;
14         ListNode pre = dummy;
15
16         int cnt = 0;
17
18         while (cur != null) {
19             cnt++;
20             if (cnt == k) {
21                 cnt = 0;
22                 pre = reverse(pre, cur.next);
23             }
24             cur = cur.next;
25         }
26
27         return dummy.next;
28     }
29
30     /**
31      * Reverse a link list between pre and next exclusively
32      * an example:
33      * a linked list:
34      * 0->1->2->3->4->5->6
35      * |           |
36      * pre        next
37      * after call pre = reverse(pre, next)
38      *
39      * 0->3->2->1->4->5->6
40      *          |  |
41      *          pre next
42      * @param pre
43      * @param next
44      * @return the reversed list‘s last node, which is the precedence of parameter next
45      */
46     private static ListNode reverse(ListNode pre, ListNode next){
47         ListNode cur = pre.next;
48
49         // record the new tail.
50         ListNode last = cur;
51         while (cur != next) {
52             ListNode tmp = cur.next;
53             cur.next = pre.next;
54             pre.next = cur;
55             cur = tmp;
56         }
57
58         last.next = next;
59         return last;
60     }

主页君的GitHub代码

ref: http://www.cnblogs.com/lichen782/p/leetcode_Reverse_Nodes_in_kGroup.html

时间: 2024-11-05 18:42:27

LeetCode: Reverse Nodes in k-Group 解题报告的相关文章

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 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-Group [024]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4648 Magic Pen 6 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1857    Accepted Submission(s): 637 Problem Description In HIT, many people have

[LeetCode] 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. 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 151. Reverse Words in a String 解题报告

[Problem] Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". Update (2015-02-12):For C programmers: Try to solve it in-place in O(1) space. [Idea] 从左往右遍历一次,每当遇到空格暂停

LeetCode Reverse Nodes in k-Group 每k个节点为一组,反置链表

题意:给一个单链表,每k个节点就将这k个节点反置,若节点数不是k的倍数,则后面不够k个的这一小段链表不必反置. 思路:递归法.每次递归就将k个节点反置,将k个之后的链表头递归下去解决.利用原来的函数接口即可,不用重新定义. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NUL

[leetcode]Reverse Nodes in k-Group @ Python

原题地址:https://oj.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

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 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: Search in Rotated Sorted Array 解题报告

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu

LeetCode Intersection of Two Linked Lists 解题报告

https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ 求两个链表的第一个公共节点,如果不存在公共节点的话就返回null. A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 解题思路: 1)如果两个链表的最后一个节点一样,那么说明两个链表一定有交点. 2)分别求出两个链表的长度,然后对长度长的链表向前移动:LengA - LengB,将两个链表进行对齐,之后一起遍历,直到找到第一个相同的节