LeetCode-24:Swap Nodes in Pairs
来源:互联网 发布:在linux上安装vim 编辑:程序博客网 时间:2024/05/28 15:26
原题描述如下:
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 maynot modify the values in the list, only nodes itself can be changed.
题意
:将单链表中的元素两两对换。
解题思路:
看代码。
Java代码:
/*** 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) {
if(head == null || head.next == null)return head;
ListNode temphead = new ListNode(0);
temphead.next = head;
ListNode tail = temphead;
ListNode first = head;
while(first != null && first.next != null){
tail.next = first.next;
first.next = first.next.next;
tail.next.next = first;
first = first.next;
tail = tail.next.next;
}
return temphead.next;
}
}
0 0
- 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
- Swap Nodes in Pairs - LeetCode 24
- LeetCode 24 - Swap Nodes in Pairs
- leetcode 24 -- Swap Nodes in Pairs
- LeetCode 24:Swap Nodes in Pairs
- leetcode-24Swap 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, Medium] Swap Nodes in Pairs
- (多校第二场1011)HDU5744 Keep On Movin
- 人生哲理
- UVA - 11234 Expressions
- Python案例-网络编程-异步解耦
- java基础笔记精简版3
- LeetCode-24:Swap Nodes in Pairs
- 本地文件的获取和解析(json,plist)
- 分治算法
- 自定义expendableListView实现与QQ联系人一样效果的通讯录
- 动态规划
- java基础笔记精简版4
- hdu2102 A计划--BFS
- 直播播控和分发基础架构
- 网