【LeetCode】24. Swap Nodes in Pairs
来源:互联网 发布:格式转换器 mac 编辑:程序博客网 时间:2024/06/05 09:37
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.
题意:给定一个链表,将其每两个元素进行对调。
思路:案例没讲清楚,实际上如果一组不足两个元素,则不操作,比如1->2->5,操作后应变为2->1->5。分析起来不麻烦,注意边界就好
代码如下:
public ListNode swapPairs(ListNode head) { if (head == null || head.next == null){ return head; } ListNode now = head, pre = null, next, newhead = head.next; while (now != null && now.next != null){ next = now.next; now.next = next.next; next.next = now; if (pre != null){ pre.next = next; } pre = now; now = now.next; } return newhead; }
阅读全文
0 0
- [LeetCode]24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs Leetcode Python
- LeetCode --- 24. Swap Nodes in Pairs
- LeetCode 24.Swap Nodes in Pairs
- [Leetcode] 24. Swap Nodes in Pairs
- 【LeetCode】24.Swap Nodes in Pairs
- [leetcode] 24.Swap Nodes in Pairs
- **LeetCode 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs LeetCode
- leetcode 24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- LeetCode--24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- 【LeetCode】24. Swap Nodes in Pairs
- [leetcode] 24. Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- LeetCode-24.Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- CGI——万法归宗
- JavaWeb之tomcat安装、配置与使用(一)
- [每日刷题记录-dxy]
- web和webApp如何显示第三方网页链接
- 反射机制(详细)
- 【LeetCode】24. Swap Nodes in Pairs
- GOOGLE PLAY更新到安卓L风格后,给运营人员的几点建议
- Java:如何用两个队列实现栈
- nagios io监控脚本
- nagios 监控的服务没有图像解决
- C语言实现单链表面试题 ----基础篇
- python 常用包总结
- C++判断字符串是否互为变形词
- java开发的几个重要事项,参考一下