【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
解法一:递归
/**
* 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 == NULL || head->next == NULL)
return head;
ListNode* newhead = head->next;
head->next = swapPairs(newhead->next);
newhead->next = head;
return newhead;
}
};
解法二:Reverse Nodes in k-Group令k=2
/**
* 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) {
return reverseKGroup(head,);
}
ListNode *reverseKGroup(ListNode *head, int k) {
ListNode* newhead = new ListNode(-);
ListNode* tail = newhead;
ListNode* begin = head;
ListNode* end = begin;
while(true)
{
int count = k;
while(count && end != NULL)
{
end = end->next;
count --;
}
if(count == )
{//reverse from [begin, end)
stack<ListNode*> s;
while(begin != end)
{
s.push(begin);
begin = begin->next;
}
while(!s.empty())
{
ListNode* top = s.top();
s.pop();
tail->next = top;
tail = tail->next;
}
}
else
{//leave out
tail->next = begin;
break;
}
}
return newhead->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* newhead = new ListNode(-);
newhead->next = head;
ListNode* tail = newhead;
while(tail->next != NULL && tail->next->next != NULL)
{
ListNode* A = tail->next;
ListNode* B = A->next; //swap
A->next = B->next;
B->next = A;
tail->next = B; tail = tail->next->next;
}
return newhead->next;
}
};
【LeetCode】24. Swap Nodes in Pairs (3 solutions)的更多相关文章
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【LeetCode】024. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【leetcode❤python】24. Swap Nodes in Pairs
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- LeetCode OJ 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- 【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 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
随机推荐
- C++常用排序法、随机数
C++常用排序法研究 2008-12-25 14:38 首先介绍一个计算时间差的函数,它在<time.h>头文件中定义,于是我们只需这样定义2个变量,再相减就可以计算时间差了. 函数开头加 ...
- mac下virtualbox安装win7系统
下载安装参考: http://win.bai-bang.top/shendu64win7.html 1.之前在win7下的virtualbox安装win7操作溜溜的,换做mac,不知道是不是太久没有安 ...
- Android中怎样调用拨打电话?
Android系统原本就为手机设计,所以,在android系统中的不论什么App中,仅仅要愿意,拨打指定电话很方便. 核心就是使用Intent跳转,指定请求Action为Intent.ACTION_C ...
- 求两个数中的较大值max(a,b)。(不用if,>)
题目:求两个数的较大值,不能使用if.>. 1.不使用if.>,还要比较大小,貌似就只能使用条件表达式: x=<表达式1>?<表达式2>:<表达式3>; ...
- C#读写txt文件的两种方法介绍[转]
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- Android4.2.2 Gallery2源码分析(4)——GLCanvas.java
首先申明,找到这个类是在GLRootView.java中发现的线索.这是一个接口,源码中对该接口作了详细的说明: // // GLCanvas gives a convenient interface ...
- kettle利用参数遍历执行指定目录下的所有对象
使用kettle设计ETL设计完成后,我们就需要按照我们业务的需要对我们设计好的ETL程序,ktr或者kjb进行调度,以实现定时定点的数据抽取,或者说句转换工作,我们如何实现调度呢? 场景:在/wor ...
- GeSHi——通用语法高亮显示
Examples Category Examples Views ActionScript 2 46173 Ada 3 27881 Apache configuration 2 40029 Apple ...
- [NPM] Execute npx commands with $npm_ Environment Variables
We will incorporate npm specific environment variables when executing various npx commands. In our e ...
- [Javascript] Wrap an API with a Proxy
Proxies allow you to use functions that haven't yet been defined on an object. This means that you c ...