34. Swap Nodes in Pairs
- Swap Nodes in Pairs My Submissions QuestionEditorial Solution
Total Accepted: 95230 Total Submissions: 270562 Difficulty: Easy
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.
Subscribe to see which companies asked this question
思路:简单,学会指针间的操作和指向
/**
* 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 *beg = head->next,*p=head,*pre=head;
while(p!=NULL&&p->next!=NULL)
{
ListNode *p_next_next = p->next->next;
pre->next = p->next;
p->next->next = p;
p->next = p_next_next;
pre = p;
p = p->next;
}
return beg;
}
};
34. Swap Nodes in Pairs的更多相关文章
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- leetcode-algorithms-24 Swap Nodes in Pairs
leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...
随机推荐
- 解决svn异常报错“”cleanup failed to process the following paths …… previous operation has not finished”
参考高票答案https://stackoverflow.com/questions/10128201/subversion-stuck-due-to-previous-operation-has-no ...
- 计算机网络传输层之TCP拥塞控制(慢开始与拥塞避免、快重传和快恢复)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105532044 学习课程:<2019王道考研计算机网络> 学习目的 ...
- [CSP-S2021] 廊桥分配
链接: P7913 题意: 有 \(m_1\) 架飞机和 \(m_2\) 架飞机停在两个机场,每架飞机有到达和离开的时间,要将 \(n\) 个廊桥分给两个机场,每个廊桥同一时刻只能停一架飞机,需要最大 ...
- Linux上的多进程间通信机制
最近项目需要一个类似于Android上广播通信机制,方便多进程间相互通信,可以是一对多,多对一,或者多对多. 大致如下 Linux现有的进程间通信能用的只有 管道 消息队列 信号量(这个还只能算同步机 ...
- 两个栈实现队列 牛客网 剑指Offer
两个栈实现队列 牛客网 剑指Offer 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. class Solution: def __init__(sel ...
- JAVA笔记3__字符串String类/对象一对一关联
import java.lang.String; import java.util.Scanner; public class Main { public static void main(Strin ...
- 浅议NetMQ常见模式和消息加密机制
浅议NetMQ常见模式和消息加密机制 概述 在传统企业级开发中,消息队列机制已经成为一种非常常见的技术实现手段,而基于NetMQ则看起来有点像一朵"奇葩",看起来从名字似乎是一个消 ...
- 关于Arrays类的静态方法asList()
Array.asList():是数组转成集合的方法 List<String> list = Arrays.asList(new String[]{"AA", " ...
- 小白都能看懂的Spring源码揭秘之IOC容器源码分析
目录 前言 IOC 只是一个 Map 集合 IOC 三大核心接口 IOC 初始化三大步骤 定位 加载 注册 总结 前言 在 Spring 框架中,大家耳熟能详的无非就是 IOC,DI,Spring M ...
- Java操作MongoDB之mongodb-driver(一)
1. mongodb-driver是mongo官方推出的java连接mongoDB的驱动包,相当于JDBC驱动. (1)通过maven仓库导入:https://mvnrepository.com/ar ...