problem:

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.

在单链表中。每两个结点交换一下位置。单个的不交换

thinking:

(1)这道题在不新建结点的情况下。指向关系复杂。慢慢分析

(2)head是头指针,指向第一个有效结点!

code:

/**
* 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 *index = head;
ListNode *pre = NULL;
ListNode *tmp = NULL;
ListNode *modify = head;;
int i=0;
if(head==NULL||head->next==NULL)
return head;
while((index!=NULL)&&(index->next!=NULL))
{
i++;
pre=index;
if(i==1)
{
head=pre->next;
index = index->next;
tmp = index->next;
pre->next = tmp;
index->next = pre;
index=tmp;
modify=pre;
}
else
{
index = index->next;
tmp = index->next;
modify->next=pre->next;
pre->next = tmp;
index->next = pre;
index=tmp;
modify=pre; } }
return head;
}
};

leetcode 题解 || Swap Nodes in Pairs 问题的更多相关文章

  1. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  2. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

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

  4. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  5. LeetCode 024 Swap Nodes in Pairs

    题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

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

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

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

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

  10. leetcode:Swap Nodes in Pairs

    Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...

随机推荐

  1. Java集合类操作优化总结

    清单 1.集合类之间关系 Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHas ...

  2. cocos2d-x学习知识点记录

    环境搭建 http://4137613.blog.51cto.com/4127613/751149 Cocos2d-x初探,HelloWorld解读 http://www.cnblogs.com/Ke ...

  3. log的6种等级

    在Java中,log有6种等级,从低到高为: (1)TRACE:用于展现程序执行的轨迹 (2)DEBUG:用于协助低层次的调试 (3)INFO:用于基本高层次的诊断信息,在长时间运行的代码段开始运行及 ...

  4. 中国首个 SaaS 模式的云告警平台 iOS 版 APP 上线

    今天上午,国内首个 SaaS 模式的云告警平台 OneAlert 正式发布 ios 版 APP,每个 ios 用户,无需电脑,都可以通过手机全程跟踪所有告警,并且可以和每一个成员一键式电话沟通,团队协 ...

  5. Android 获取系统短信内容

    //这里通过内容提供者获取系统短信内容 Uri uri = Uri.parse("content://sms/"); String[] projection = {"_i ...

  6. java中的单例模式与doublecheck

    转自: http://devbean.blog.51cto.com/448512/203501 在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单 ...

  7. Windows编程中的堆管理(过于底层,一般不用关心)

    摘要: 本文主要对Windows内存管理中的堆管理技术进行讨论,并简要介绍了堆的创建.内存块的分配与再分配.堆的撤销以及new和delete操作符的使用等内容. 关键词: 堆:堆管理 1 引言 在大多 ...

  8. http实现发送post请求,获取结果集

    package com.ming; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Ou ...

  9. pcDuino无显示器刷机与使用

    准备工作: pcduino : 点此购买 一个可用的并且启用了DHCP的有线路由器. 连接pcduino与路由器的网线一根. 至少1张4G microSD卡,如果内存卡不大,可以用内存卡刷内核,用u盘 ...

  10. ♫【MV*】

    MVC数据(模型) 展现层(视图) 用户交互层(控制器) MV*MVW, MVVM, MVC JavaScript MVC框架PK:Angular.Backbone.CanJS与Ember JavaS ...