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-& ...
随机推荐
- maven PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path
maven编译的时候遇到的奇葩问题, 非常奇葩, 所有其他同事都没有遇到 , 仅仅是我遇到了 不清楚是因为用了最新的JDK的缘故(1.8 update91)还是其他什么原因. 总之是证书的问题. 当 ...
- 让Chrome可以修改字体
在chrome地址栏输入chrome://flags/ , 然后将"停用DirectWrite Windows"改为停用 , 这样自定义的字体就可以生效了.
- gerrit 配置 apache2 反向代理(转载)
Apache 2 Configuration To run Gerrit behind an Apache server using mod_proxy, enable the necessary A ...
- Python应用02 Python服务器进化
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! **注意,在Python 3.x中,BaseHTTPServer, SimpleH ...
- HTTP Response中的Status-Code
1XX : 通知信息.请求被接收,继续处理2XX : 成功.请求动作被成功接收.理解和接受3XX : 重定向.需要其他的动作来完成这个请求4XX : 客户端错误.请求包含错误的语法或者缺少语法5XX ...
- Scroll滚动后发生的改变
条件:一个panel,足以让panle产生滚动条的N多控件. 动作:拖动滚动条. 影响:呈现在当前panle视图中的控件的Location.Y或Top值>=0,隐藏在滚动条上方的控件的Locat ...
- 华为ICDcomm接口js测试
1)获取坐席状态接口调用方法: “方法”------“坐席与班组” 中的 Phone.QueryAgentStatusEx(工号)如果该方法调用成功,那么坐席状态将会存入到属性: Phone.Agen ...
- JAVA中集合输出的四种方式
在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...
- delphi的tserversocket控件如何接收16进制数
http://bbs.csdn.net/topics/390473005 对方客户端发送数据如:68 00 00··········:接收完成后,数据长度没错(13),但是显示接收结果时,只显示一个字 ...
- C++primer 练习11.33:实现你自己版本的单词转换程序
// 11_33.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...