leetcode 24

链表操作的,要注意标记头结点和边界问题。
代码如下:
ListNode *swapPairs(ListNode *head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* p=head;
ListNode* q=head->next;
ListNode* x=head->next->next;
ListNode* t=NULL;
head=q;
while(p!=NULL&&q!=NULL){
q->next=p;
p->next=NULL;
if(t!=NULL)
t->next=q;
t=p;
p=x;
if(x!=NULL)
q=x->next;
if(q!=NULL)
x=q->next;
}
if(p!=NULL)
t->next=p;
return head;
}
更加巧妙的解法,直接对链表中节点的val进行交换,不用操作链表;
代码如下:
/**
* 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) {
int n;
ListNode* node = head;
while (head != NULL && head->next != NULL) {
n = head->val;
head->val = head->next->val;
head->next->val = n;
head = head->next->next;
}
return node;
}
};
leetcode 24的更多相关文章
- [LeetCode] 24 Game 二十四点游戏
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- [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 ...
- Java实现 LeetCode 24 两两交换链表中的节点
24. 两两交换链表中的节点 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3-&g ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- Leetcode: 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...
- leetcode 24. 两两交换链表中的节点 及 25. K 个一组翻转链表
24. 两两交换链表中的节点 问题描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2-> ...
- [leetcode 24] Swap Nodes in k-Group
1 题目: 目前被墙,看不了. 2 思路: 比较简单,注意处理边界点就好 3 代码: public ListNode swapPairs(ListNode head) { int temp; if(h ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
随机推荐
- BOM(Bill of Material)详解
一.物料(ITEM) 物料(Item or Material),是对存货的统称,是建立BOM和其他业务数据的前提条件,在ERP系统中称之为物料主数据,包括原材料(Raw material).在产品(W ...
- JS常用各种正则表达式
1.非负整数 /^\d+$/ 2.正整数 /^[0-9]*[1-9][0-9]*$/ 3.非正整数 /^((-\d+)|(0+))$/ 4.负整数 ...
- eclipse中安装tomcat插件
一.软件下载 Eclipse3.6 IDE for Java EE Developers: 下载地址:http://eclipse.org/downloads/ Tomcat Eclipse Plug ...
- 已知ip地址和其子网掩码如何求网络号子网号主机号
已知ip地址为10.130.89.95,其子网掩码为255.255.255.224,求其网络号.子网号和主机号. 要看子网掩码变长在第几节,255.255.255.224是在第四节借了位 把224转换 ...
- shell如何传递外部参数给文件
shell里面如何传递参数: sh test.sh zhang 20 那test.sh里面咋接受参数呢? #!/usr/bin/env sh name=$1 age=$2 echo "nam ...
- DEDE仿站经常用到的基本标签和变量
一.针对于DEDE后台基本设置里面的使用到的数据标签. 主标题:{dede:global.cfg_webname/} 主要用于<title></title>里面 网 站描述: ...
- 使用Nlog记录日志到数据库
Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台,保存到文本,也可以很方便的记录到数据库中. 可以在这里下载Nlog:http://nlog-project.org/ 这里分享一下 ...
- rand()和srand()区别
标准库<cstdlib>提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始,返回一个[seed, RAND_MAX(0x ...
- [ActionScript 3.0] AS3.0 本机鼠标指针
Flash Player 10.2添加了内置的本机鼠标指针(native mouse cursor)支持,虽然在之前的版本里我们可以侦听MouseEvent事件来模拟鼠标指针,但是在有了原生的本机鼠标 ...
- [ActionScript 3.0] AS3.0 对象在一定范围随机显示不重叠
import flash.geom.Rectangle; import flash.display.MovieClip; import flash.display.Sprite; var arr:Ar ...