LeetCode_OJ【24】Swap Nodes in Pairs
来源:互联网 发布:linux安装jira7.22 编辑:程序博客网 时间:2024/06/04 19:13
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.
这种题目是很基础的,但是还是得细心,最好在纸上给要操作的节点画出来。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode swapPairs(ListNode head) {ListNode dummy = new ListNode(-1);dummy.next = head;ListNode pre = dummy;while(pre.next!=null && pre.next.next != null){ListNode cur1 = pre.next;ListNode cur2 = cur1.next;pre.next = cur2;cur1.next = cur2.next;cur2.next = cur1;pre = cur1;} return dummy.next; }}
0 0
- LeetCode_OJ【24】Swap Nodes in Pairs
- [24]Swap Nodes in Pairs
- 24、Swap Nodes in Pairs
- 24 Swap Nodes in Pairs
- #24 Swap Nodes in Pairs
- 24 Swap Nodes in Pairs
- 24 - Swap Nodes in Pairs
- 24 Swap Nodes in Pairs
- [24]Swap Nodes in Pairs
- [24] Swap Nodes in Pairs
- 【24】Swap Nodes in Pairs
- 24-Swap Nodes In Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes In Pairs
- Swap Nodes in Pairs
- ubi 的相关概念和解释
- 中国剩余定理
- CentOS──xxx is not in the sudoers file解决方法
- long、string和CString类型转换,CString转double、char *
- javascript的最佳实践
- LeetCode_OJ【24】Swap Nodes in Pairs
- Sensu初探
- C算法-求s=a+aa+aaa+aaaa+aa...a的值
- 在Android中使用Junit测试注意事项
- Android+JNI 的编写步骤 从HelloWorld开始
- C# 下载远程文件
- MFC Radio Button 使用 分组 RadioButton 用法
- 用cxf编写基于spring的webservice之下篇
- Android Dalvik虚拟机JNI方法的注册过程分析