LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
Given ->->->, you should return the list as ->->->.
奇数位和偶数位互换,若是奇数个,不用管最后一个
解法一:(C++)
ListNode* swapPairs(ListNode* head) {
if(!head)
return NULL;
ListNode* dummy=new ListNode(-),*cur=dummy;
dummy->next=head;
while(cur->next&&cur->next->next){
ListNode* t=cur->next->next;
cur->next->next=t->next;
t->next=cur->next;
cur->next=t;
cur=t->next;
}
return dummy->next;
}
java:
public ListNode swapPairs(ListNode head) {
if(head==null)
return null;
ListNode dummy=new ListNode(-1),pre=dummy;
dummy.next=head;
while(pre.next!=null&&pre.next.next!=null){
ListNode t=pre.next.next;
pre.next.next=t.next;
t.next=pre.next;
pre.next=t;
pre=t.next;
}
return dummy.next;
}
方法二:使用递归的方法,递归遍历到末尾两个,然后交换末尾两个,依次往前遍历(C++)
ListNode* swapPairs(ListNode* head) {
if(!head||!head->next)
return head;
ListNode* t=head->next;
head->next=swapPairs(head->next->next);
t->next=head;
return t;
}
LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java的更多相关文章
- [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 ...
- 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] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- (链表 递归) 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 ...
- [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 ...
随机推荐
- linux自动备份lampp的日志
要求是:每天23:59自动备份lampp的日志/opt/lampp/logs/access_log 放到这个目录下/tmp/bak 命名格式:access_log.日期 1.新建shell脚本#vi ...
- 软件开发者路线图梗概&书摘chapter5
恒久学习:整个职业生涯,反馈回路,了解弱点 1.提高带宽:多维度.高效获取知识 博客.关注twitter动态.订阅邮件列表.加入本地用户组.技术大会.联系书的作者.在线教程 从信息的海洋中回到实际软件 ...
- kali在执行 apt-get update 命令时报错的解决方法
报错内容: root@kali:~# apt-get updateGet:1 http://kali.mirror.garr.it/mirrors/kali kali-rolling InReleas ...
- git 创建删除分支
进入仓库目录 -- 查看本地分支 git branch -- 查看远程分支 git branch -r -- 查看远程和本地的分支 git branch -a -- 下载远程分支代码,并切换到分支 进 ...
- 个人简历html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 1080P60视频源---verilog
1080P60视频源---verilog `timescale 1ns / 1ps ////////////////////////////////////////////////////////// ...
- ARM 汇编学习笔记
- Oracle 外键级联更新
Oracle数据库中,外键约束只允许级联删除,不允许级联更新,因此,如果想要实现主表数据更新后,子表外键自动更新,只能取消外键关系,通过前端程序来维护实现完整引用,一个代替的解决方案是使用延迟约束和触 ...
- JavaScript获取元素CSS计算后的样式
原文链接https://www.w3ctech.com/topic/40 我们在开发过程中,有时候需要根据元素已有样式来实现一些效果,那我们应该如何通过JavaScript来获取一个元素计算后的样式值 ...
- Functional programming idiom
A functional programming function is like a mathematical function, which produces an output that typ ...