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->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; * struct ListNode *next; * }; */ struct ListNode* swapPairs(struct ListNode* head) { struct ListNode *tmp, *new_head = head, *tail = NULL; while(NULL != head&&NULL != head->next){ tmp = head->next; head->next = head->next->next; tmp->next = head; if(head == new_head){ new_head = tmp; } else{ tail->next = tmp; } tail = head; head = head->next; } if(NULL != head&&NULL != tail){ tail->next = head; } return new_head; }
LeetCode OJ 24. Swap Nodes in Pairs的更多相关文章
- [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 (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【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 OJ】Swap Nodes in Pairs
题目:Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2 ...
- LeetCode OJ: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. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- 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(M);25. Reverse Nodes in k-Group(H)
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
随机推荐
- Unicode String to a UTF-8 TypedArray Buffer in JavaScript
https://coolaj86.com/articles/unicode-string-to-a-utf-8-typed-array-buffer-in-javascript/
- Round545div2B(1138B)
一.题目链接 https://codeforces.com/problemset/problem/1138/B 二.思路 贪心是肯定不行的. 设会$[1,0]$的人存在容器$p_1$里面,会$[0,1 ...
- 由于ip改变重新配置CM集群
修改所有主机/etc/hosts 修改所有agent节点的/opt/cm-5.5.1/etc/cloudera-scm-agent/config.ini,中server的ip 主节点启动cm serv ...
- 学习js第一天小结
1.JavaScript的书写方式: <script type="text/javascript"> </script> <script sr ...
- Android Notification通知栏的使用,通知栏在Android8.0系统不显示的问题;
1.正常使用通知栏: /** * 创建通知栏管理工具 */ NotificationManager notificationManager = (NotificationManager) getSys ...
- Python ————反射机制
python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr,改四个函数分别用于对对象内部执行:检查是否含有某成员.获取成员.设置成员.删除成员. ...
- angularjs的ng-class
<!--第一种 直接加变量--> <div ng-class="tempClass"></div> <!--第二种 用{{}} 包住的变量 ...
- Linux 安装redis 基本配置 发布订阅,安全配置,持久化 rdb ,aof
redis redis相关配置1.yum 源码 rpm yum 快速,间接,高效,解决依赖关系,(自动安装到某个路径,不可控),通过yum安装的软件查询命令 rpm -ql nginx yum源 ...
- django模型之meta使用
模型元数据Meta是“任何不是字段的数据”,比如排序选项(ordering),数据库表名(db_table)或者人类可读的单复数名称(verbose_name 和verbose_name_plural ...
- Html标签及各种属性(持续更新)
<!--Html标签属性之:required (必须的)--> <input type="file" id="Images" required ...