24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
Given a linked list, swap every two adjacent nodes and return its head. For example,
Given ->->->, you should return the list as ->->->. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
- Total Accepted: 156137
- Total Submissions: 413794
- Difficulty: Medium
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if (head == nullptr || head->next == nullptr) return head;
ListNode dummy(-);
dummy.next = head;
for(ListNode *prev = &dummy, *cur = prev->next, *next = cur->next;
next;
prev = cur, cur = cur->next, next = cur ? cur->next: nullptr) {
prev->next = next;
cur->next = next->next;
next->next = cur;
}
return dummy.next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head)
{
ListNode dummy(-);
dummy.next = head;
ListNode *p = &dummy, *q = head;
while (q && q->next)
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
p = q;
q = q->next;
}
return dummy.next;
}
};
3m 37.45%
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 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: ->->->-> For k = , you should return: ->->->-> For k = , you should return: ->->->->
- Total Accepted: 89044
- Total Submissions: 294580
- Difficulty: Hard
解题思路:节点指针的指向变换比较麻烦.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution { //by guxuanqing@gmail.com
public:
ListNode* reverseKGroup(ListNode* head, int k) {
int len = ;
ListNode dummy(-);
dummy.next = head;
if(k == ) return dummy.next;
ListNode *p = &dummy, *prevq = &dummy, *q = head;
while (p->next) {
p = p->next;
++len;
}
p = &dummy;
div_t dv = div(len, k);
int shang = dv.quot; while (shang--)
{
ListNode *tmpp = q;
prevq = q;
q = q->next;
int tmpk = k - ;
while (tmpk--)
{
ListNode *qq = q->next;
q->next = prevq;
prevq = q;
q = qq;
}
tmpp->next = q;
p->next = prevq;
p = tmpp;
prevq = tmpp;
}
return dummy.next;
}
};
26ms 31.72%
/**
* 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 == nullptr || head->next == nullptr || k < ) return head;
ListNode dummy(-);
dummy.next = head;
for(ListNode *prev = &dummy, *end = head; end; end = prev->next) {
for (int i = ; i < k && end; i++)
end = end->next;
if (end == nullptr) break;
prev = reverse(prev, prev->next, end);
}
return dummy.next;
}
ListNode* reverse(ListNode *prev, ListNode *begin, ListNode *end) {
ListNode *end_next = end->next;
for (ListNode *p = begin, *cur = p->next, *next = cur->next;
cur != end_next;
p = cur, cur = next, next = next ? next->next : nullptr) {
cur->next = p;
}
begin->next = end_next;
prev->next = end;
return begin;
}
};
26ms
24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)的更多相关文章
- [Leetcode][Python]25: Reverse Nodes in k-Group
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...
- Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)
Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...
- [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 ...
- 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- 【LeetCode】25. Reverse Nodes in k-Group (2 solutions)
Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ret ...
- 25.Reverse Nodes in k-Group (List)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- 25. Reverse Nodes in k-Group (JAVA)
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k ...
- LeetCode 25 Reverse Nodes in k-Group Add to List (划分list为k组)
题目链接: https://leetcode.com/problems/reverse-nodes-in-k-group/?tab=Description Problem :将一个有序list划分 ...
- 24. Swap Nodes in Pairs + 25. Reverse Nodes in k-Group
▶ 问题:单链表中的元素进行交换或轮换. ▶ 24. 每两个元素进行翻转.如 [1 → 2 → 3 → 4 → 5] 变换为 [2 → 1 → 4 → 3 → 5] ● 初版代码,4 ms class ...
随机推荐
- WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法。
原文:WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_4330 ...
- 【php增删改查实例】第六节 - 部门管理模块(开始)
sql建表语句详见:https://www.jianshu.com/p/c88077ed9073 1.新建html模板 新建一个空白的txt文档,然后把后缀名改为.html 用任意一个编辑器打开,比如 ...
- [APIO2015]巴厘岛的雕塑[按位贪心+dp]
题意 给你长度为 \(n\) 的序列,要求分成 \(k\) 段连续非空的区间,求所有区间和的 \(or\) 最小值. 分析 定义 \(f_{i,j}\) 表示前 \(i\) 个点分成 \(j\) 段的 ...
- R绘图 第九篇:绘制散点图和气泡图(ggplot2)
绘制散点图(scatterplots)使用geom_point()函数,气泡图(bubblechart)也是一个散点图,只不过点的大小由一个变量(size)来控制.散点图潜在的最大问题是过度绘图:当一 ...
- 委托、多播委托(MulticastDelegate)
委托.多播委托(MulticastDelegate) 多播委托(MulticastDelegate)继承自 Delegate ,表示多路广播委托:即,其调用列表中可以拥有多个元素的委托.实际上,我们自 ...
- allegro 封装 (引脚编号修改)
1. 打开dra文件在find里面 off all 然后只点击text 2.点击需要更改的焊盘 3.菜单栏edit - text 4.弹出窗口修改即可
- 更改jenkins的默认工作空间并迁移插件和配置数据
最近刚使用阿里云ECS centos服务器,购买的是40G的系统盘,60G的数据盘. 昨天在查看服务器磁盘空间的时候,偶然发现 /dev/vda1 下面40G的空间已使用17G, 因为服务器才开始使用 ...
- Nuxeo 认证绕过和RCE漏洞分析(CVE-2018-16341)
简介 Nuxeo Platform是一款跨平台开源的企业级内容管理系统(CMS).nuxeo-jsf-ui组件处理facelet模板不当,当访问的facelet模板不存在时,相关的文件名会输出到错误页 ...
- 《Linux内核分析》 之 计算机是如何工作的
[李行之原创作品 转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] <Linux内 ...
- beta版本“足够好”/测试矩阵
能通过地图鱼相应的地点信息实时交互,便于用户操作. 测试矩阵