24. Swap Nodes in Pairs

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%

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.

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)的更多相关文章

  1. [Leetcode][Python]25: Reverse Nodes in k-Group

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 25: Reverse Nodes in k-Grouphttps://oj. ...

  2. Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表)

    Leetcode 25. Reverse Nodes in k-Group 以每组k个结点进行链表反转(链表) 题目描述 已知一个链表,每次对k个节点进行反转,最后返回反转后的链表 测试样例 Inpu ...

  3. [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  ...

  4. 蜗牛慢慢爬 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. ...

  5. 【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 ...

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

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

  8. 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划分 ...

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

随机推荐

  1. 【第五课】LNMP环境的入门

    目录 一. LNMP环境介绍 二.Mysql的二进制免编译安装 三.PHP 7.2.5编译部署 四.Nginx的编译安装 五.YUM安装Nginx 一. LNMP环境介绍 LNMP(Linux + N ...

  2. ubuntu12.04安装OVS

    1.下载openVswitch ovs官网 2.运行如下脚本 #!/bin/bash cd /home/sdn/ovs/openvswitch- rm /usr/local/etc/openvswit ...

  3. Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程

    Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...

  4. MODIS 数据产品预处理

    MODIS 数据产品预处理 1  MCTK重投影 第一步:安装ENVI的MCTK扩展工具 解压压缩包,将其中的mctk.sav与modis_products.scsv文件复制到如图所示,相应的ENVI ...

  5. 使用plumbing命令来深入理解git add和git commit的工作原理

    前言: plumbing命令 和 porcelain命令 git中的命令分为plumbing命令和porcelain命令: porcelain命令就是我们常用的git add,git commit等命 ...

  6. petapoco 对存储过程的扩展 干货

    好久没发表文章了.心血来潮,简单的介绍下这次工作中的问题. 项目中运用了Petapoco,可是petapoco对存储过程的支持不够好.或者说对于某些特殊场景,petapoco的sql支持度有限. 比如 ...

  7. TeamWork#3,Week5,Release Notes of the Alpha Version

    在这里的是一款你时下最不可或缺的一款美妙的产品. “今天哪家外卖便宜?” “今天这家店在哪个网站打折?” “这家店到底哪个菜好吃?” 这些问题你在寝室/办公室每天要问几次?还在为了找一家便宜的外卖店而 ...

  8. 12.17daily_scrum

    悬浮窗的优化设计工作已经展开,各界面的测试也在有条不紊的进行,大家都尽量做到了在发现了软件自身的一些问题和bug后,做到在第一时间及时恢复和修改,工作进度的安排也在预期之中,明日的任务发布如下: 具体 ...

  9. sql两个日期之间的查询统计

    sql查询统计 sql语句: select count(code) as '统计',create_time as '订单时间' from sp_orders where datediff(create ...

  10. vue-resource和axios区别

    vue-resource Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没有必要引入jQuery.vue-resource是Vue.js的 ...