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. Caffe上手教程

    Caffe上手教程 入门系列FAQ72 在Unbuntu上安装Caffe828 Windows下安装Caffe1.4K Caffe框架上手教程1.2K Caffe编译运行调试462 Caffe 电脑配 ...

  2. REST-framework快速构建API--初体验

    一.快速上手 1.环境准备 安装restframework,注册app pip install djangorestframework INSTALLED_APPS = [ 'django.contr ...

  3. 微信小程序获取客户端系统信息

    微信小程序中有个API: wx.getSystemInfo() 可以获取系统的信息 wx.getSystemInfoSync()===>同步获取系统信息 wx.getSyatemInfo({ s ...

  4. NameNode内存溢出和DataNode请求超时异常处理

    问题背景 春节假期间,接连收到监控程序发出的数据异常问题,赶忙连接上跳板机检查各服务间的状态,发现Datanode在第二台.第三台从节点都掉线了,通过查看Datanode和Namenode运行日志,发 ...

  5. SCRUM 12.09 软件工程第二周计划

    第二轮迭代的第二周开始了,上一周我们进行了对代码优化的探索与自我审查. 本周,我们有以下两点目标要实现: 1.对客户端进行优化. 2.网络爬虫爬取美团外卖. 客户端优化主要开发人员:高雅智.牛强.彭林 ...

  6. 第一次Spring会议成果意见汇总

    第一组:9-625  只看到了界面,而两台笔记本电脑通过局域网进行通信的功能则没有看到,没有看到实质性的成果.他们的软件还是非常实用的,它仅通过局域网就能通信,大大减少了流量费用,提高了通信效率,希望 ...

  7. The role of the inter-controller consensus in the placement of distributed SDN controllers

    2017 Computer Communications 问题:in-band网络的多控制器放置问题,考虑到多个控制器之间的同步(Ctr-Ctr)可能影响到控制器与交换机(Ctr-Sw)的时延: 关于 ...

  8. Fast R-CNN论文阅读笔记

    论文地址:Fast R-CNN R-CNN的缺陷 (1)训练是一个多级的流水线.R-CNN首先在候选目标上微调一个卷积神经网络,使用log loss.然后使用SVMs充当目标分类器,以取代softma ...

  9. 更新pip10后 ImportError: cannot import name ‘main'

    百度了几个回答都没有解决问题,有些回答明显是直接复制过来的一点价值都没有,然后google一下立马解决.很多时候不能怪搜索引擎,问题出在一些国内网友对知识的不负责任 解决:找到报错文件,也就是那个pi ...

  10. 《Bandwidth-Aware Scheduling With SDN in Hadoop:A New Trend for Big Data》--2017

    Hadoop中使用SDN的带宽感知调度:大数据的一种新趋势 Abstract: 为了处理大规模的数据,提出了基于Hadoop框架的MapReduce,在Hadoop系统中,有一种叫做NP完全最小(NP ...