LeetCode OJ--Swap Nodes in Pairs
https://oj.leetcode.com/problems/swap-nodes-in-pairs/
链表的处理
/**
* 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 = new ListNode(-);
if(head == NULL)
return dummy->next; dummy->next = head; ListNode *tail = dummy; ListNode *current = head;
ListNode *second = head->next;
while(current && second)
{
tail->next = second;
current->next = second->next;
second->next = current; current = current->next;
if(current == NULL)
break;
second = current->next;
tail = tail->next;
tail = tail->next; }
return dummy->next;
}
};
LeetCode OJ--Swap Nodes in Pairs的更多相关文章
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- LeetCode 024 Swap Nodes in Pairs
题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- leetcode 【 Swap Nodes in Pairs 】python 实现
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- [LeetCode] 24. Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Java for LeetCode 024 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- leetcode:Swap Nodes in Pairs
Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...
随机推荐
- Python基础篇【第2篇】: Python内置函数(一)
Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...
- [转] 解决HttpServletResponse输出的中文乱码问题
首先,response返回有两种,一种是字节流outputstream,一种是字符流printwrite. 申明:这里为了方便起见,所有输出都统一用UTF-8编码. 先说字节流,要输出“中国" ...
- 同一台服务器启动多个driver负载机实例
COSBench添加driver负载机 说明:Driver是COSBench测试工具中对负载机的一种标记,相当于loadrunner中的负载发生器. 在进行测试时,不管出于什么原因,我有时候就想单台服 ...
- vbs脚本要求在cmd中输入输出用StdIn ,StdOut
Dim StdIn, StdOutSet StdIn = WScript.StdInSet StdOut = WScript.StdOut Do While Not StdIn.AtEndOfStre ...
- leetcode 160
160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of ...
- CSS子元素居中(父元素宽高已知,子元素未知)
<style> .container{width:400px; height:400px; position:relative;} .center{position:absolute; l ...
- Support Vector Machine (1) : 简单SVM原理
目录 Support Vector Machine (1) : 简单SVM原理 Support Vector Machine (2) : Sequential Minimal Optimization ...
- delphi 10 seattle 中 解决IOS 9 限制使用HTTP 服务问题
IOS 9 于17号早上正式开始推送,早上起来立马安装,这次升级包只有1G, 安装空间也大大降低(想起IOS 8 升级时,几乎把手机里面的东西删光了,满眼都是泪). 虽然安装后,网上几乎是铺天盖地的吐 ...
- TJI读书笔记11-多态
TJI读书笔记11-多态 再说说向上转型 多态的原理 构造器和多态 协变返回类型 使用继承进行设计 多态是数据抽象和继承之后的第三种基本特征. 一句话说,多态分离了做什么和怎么做(再次对埃大爷佩服的五 ...
- 关于ES、PES、PS/TS 码流
一.基本概念 )ES ES--Elementary Streams (原始流)是直接从编码器出来的数据流,可以是编码过的视频数据流(H.264,MJPEG等),音频数据流(AAC),或其他编码 ...