Swap Nodes In Pairs
来源:互联网 发布:c tryparse 源码 编辑:程序博客网 时间:2024/05/08 06:38
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.
public ListNode swapPairs(ListNode head) {// Start typing your Java solution below// DO NOT write main() functionif(head == null)return head;if(head.next == null)return head;ListNode odd = head;ListNode even = head.next;ListNode pre = head;odd.next = even.next;even.next = odd;head = even;pre = odd;while(odd.next != null && odd.next.next != null){odd = odd.next;even = odd.next;odd.next = even.next;even.next = odd;pre.next = even;pre = odd;}return head;}
- 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
- 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
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- Swap Nodes in Pairs
- 基于FBReader的andriod电子书模板
- call_user_func_array()的用法
- 如果判断数组是一维还是二维?
- Counting Unique k-mers -- My First Go Program
- PIC18f46j50单片机里SPI部分的一点程序
- Swap Nodes In Pairs
- 2013年重新回到csdn 但这次我要重新学习java
- AI TOOL Lab
- 将excel中的数据快速插入表中
- [跟我学ROS]4.编译ROS系统中的功能包
- visual 继承当前被禁用,因为基类引用设备特定的组件或包含 p/invoke(转)
- 什么是Java的弱引用(Weak Reference)
- hanoi
- 10 Best Web Hosting Providers