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 ...
随机推荐
- Centos 7 安装mysql5.7.24二进制 版本
Mysql 二进制安装方法 下载mysql https://dev.mysql.com/downloads/mysql/ 1.解压包 tar xf mysql-5.7.24-linux-glibc2. ...
- vsftp在防火墙开启需要开放的端口
1.开放tcp端口 firewall-cmd --zone=public --add-port=20/tcp --permanent firewall-cmd --zone=public --add- ...
- Python列表知识点讲解
增删改查 增 X.append函数是在原有列表中的末尾追加一个新的元素存放在列表中 X.extend() 将一个列表中的元素添加到另一个列表中,将所引用的原列表保持不变,同时extend还可以运用到, ...
- unity2D背景移动补偿从而获得3d错觉效果
2d平台跳跃游戏当相机移动的时候背景跟随进行微调移动,从而使得玩家获得3d的错觉 using System.Collections;using System.Collections.Generic;u ...
- 小程序swiper组件高度自适应【转载】
最近在做小程序开发,复制官方文档上的swiper组件实测后发现,图片不能自适应.网上找了几个版本测试都发现存在一些小问题,目前这个版本本人实测是最好用的.记录一下,方便日后使用. 感谢原创大神的帮助, ...
- MIT-6.828-JOS-lab3:User Environments
Lab 3: User Environments实验报告 tags:mit-6.828 os 概述: 本文是lab3的实验报告,主要介绍JOS中的进程,异常处理,系统调用.内容上分为三部分: 用户环境 ...
- Golang Context 详细介绍
Golang context 本文包含对context实现上的分析和使用方式,分析部分源码讲解比价多,可能会比较枯燥,读者可以直接跳过去阅读使用部分. ps: 作者本着开源分享的精神撰写本篇文章,如果 ...
- LeetCode-765.情侣牵手
N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并肩坐在一起. 一次交换可选择任意两人,让他们站起来交换座位. 人和座位用 0 到 2N-1 的整 ...
- PAT甲题题解-1102. Invert a Binary Tree (25)-(建树,水题)
就是把输入给的左孩子右孩子互换一下,然后输出层次遍历和中序遍历. #include <iostream> #include <algorithm> #include <c ...
- Hybrid APP基础篇(二)->Native、Hybrid、React Native、Web App方案的分析比较
说明 Native.Hybrid.React.Web App方案的分析比较 目录 前言 参考来源 前置技术要求 楔子 几种APP开发模式 概述 Native App Web App Hybrid Ap ...