LeetCode_链表操作1—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.
ListNode *swapPairs(ListNode *head)
{
if(head==NULL) return head;
int flag = 0;
ListNode *p = head;
ListNode *q = p->next;
while(q!=NULL)
{
p->next = q->next;
q->next = p;
if(flag==0)//第一次操作时保存头指针
{
head = q;
}
flag = 1;
ListNode *pre = p;//记录P
//p后移,q指向p的后继,如果p==NULL,p=p->next;报错,此时只需要直接
//将q赋NULL
p = p->next;
if(p!=NULL)
q = p->next;
else
q = NULL;
//若前面的p=p->next使得q==NULL,就不对了
if(q!=NULL)
pre->next = q;
}
return head;
}
关于链表的操作比较麻烦,虽然不会涉及到动态规划、搜索等复杂的算法思想,但是指针操作特别容易出错,面试或者笔试时,不容易准确快速的写出没有bug的代码,唯有平时好好总结,没事多看几遍啦。。。
LeetCode_链表操作1—Swap Nodes in Pairs的更多相关文章
- Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转
问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [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 (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- leetcode-algorithms-24 Swap Nodes in Pairs
leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...
随机推荐
- Unix系统编程()深入探究文件IO概述
open调用将引入原子atomicity操作的概念. 将某一系统调用所要完成的各个动作作为不可中断的操作,一次性加以执行. 原子操作是许多系统调用得以正确执行的必要条件. 还介绍一个系统调用fcntl ...
- lockf函数的使用
#include<stdio.h> #include<unistd.h> void main() {int p1,p2,i; while((p1=fork())==-1);// ...
- linux回调函数的使用
#include<stdio.h> #include<pthread.h> #include<unistd.h> pthread_mutex_t mutex; pt ...
- EF常用查询语句
//方法一 Linq to Entities var info = from p in entity.Users where p.ID >= 10 orderby p.ID ...
- WINDOWS中设置计划任务执行PHP文件
1.写一个PHP程序,命名为test.php,内容如下所示: <? $fp = fopen("test.txt", "a+"); fwrite($fp, ...
- 按SCI影响因子排序的前50人工智能期刊列表
附录二:按SCI影响因子排序的前50人工智能期刊列表 出版物名称,影响因子 IEEE TRANSACTIONS ON FUZZY SYSTEMS, 6.701 International Jou ...
- 【BZOJ】1629: [Usaco2007 Demo]Cow Acrobats(贪心+排序)
http://www.lydsy.com/JudgeOnline/problem.php?id=1629 这题我想了很久都没想出来啊... 其实任意两头相邻的牛交换顺序对其它牛是没有影响的.. 那么我 ...
- ThinkPHP项目笔记之RBAC(权限)上篇
当理清这5个表的关系,接下来,就是功能介绍了.
- 求出每个team粉丝数最多的3个国家
有这么个表 fans(team,nationality,fanCount) 'Barcelona','Germany',12000'Barcelona','Spain',18000'Barcelona ...
- Leetcode: Palindrome Partition I II
题目一, 题目二 思路 1. 第一遍做时就参考别人的, 现在又忘记了 做的时候使用的是二维动态规划, 超时加超内存 2. 只当 string 左部分是回文的时候才有可能减少 cut 3. 一维动规. ...