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. 完美卸载SQL Server 2008的方案

    转自完美卸载SQL Server 2008的方案 针对SQL数据库卸载不完全的现象,做了如下总结:   1,控制面板 卸载   首先,打开控制面板,按照"安装时间"进行排序,卸载S ...

  2. OneAlert 入门(四)——事件分派和通知必达

    OneAlert 是国内首个 SaaS 模式的云告警平台,集成国内外主流监控/支撑系统,实现一个平台上集中处理所有 IT 事件,提升 IT 可靠性.有了 OneAlert,你可以更快更合理地为事件划分 ...

  3. Android ListView异步加载数据

    1.主Activity public class MainActivity extends Activity { private ListView listView; private ArrayLis ...

  4. Php使用sqlite

    php sqlite文档:http://php.net/manual/en/book.sqlite.php sql:http://www.php100.com/html/webkaifa/PHP/PH ...

  5. Android日期时间格式国际化

    公共类 的DateFormatSymbols 扩展对象 实现 Serializable接口 Cloneable接口 java.lang.Object的    ↳ java.text.DateForma ...

  6. bzoj1927

    看到这道题不难想到费用流吧,但是怎么做呢? 一开始看到“每个点都恰好走一次”,我首先想到的有下界最小费用流, 然后发现这没有满足最大流的条件,然后又连边松弛掉多余的流 为了按照可行流的做法先减减去极大 ...

  7. Makefile中include、-include、sinclude的区别

    如果指示符“include”指定的文件不是以斜线开始(绝对路径,如/usr/src/Makefile...),而且当前目录下也不存在此文件:make将根据文件名试图在以下几个目录下查找:首先,查找使用 ...

  8. jquery方法详解--bind(type, [data], fn)

    转自:http://www.zhufengpeixun.cn/jquery/bind_type_data_fn.html bind(type, [data], fn)  返回值::jQuery 概述 ...

  9. Using Nini .NET Configuration Library

    Using Nini .NET Configuration Library Tweet When developing a desktop application, there will be tim ...

  10. android自动化(2)

    使用monkeyrunner 自动化删除程序的时候出现如下错误 在任务管理器卸载android adb,Ok..然后再次使用的时候就出现这个问题, Try below steps: Close the ...