24. Swap Nodes in Pairs(每两个相邻节点进行一次交换)
来源:互联网 发布:数据人生txt全集下载 编辑:程序博客网 时间:2024/05/22 07:48
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) { if(head==null||head.next==null) return head; ListNode p=head,h=p,q=head.next; p.next=q.next; q.next=p; head=q; if(p.next!=null){ p=p.next; q=p.next; }else return head; while(q!=null){ p.next=q.next; q.next=p; h.next=q; h=p; p=p.next; if(p!=null) q=p.next; else return head; }return head; }}
0 0
- 24. Swap Nodes in Pairs(每两个相邻节点进行一次交换)
- Swap Nodes in Pairs 链表交换相邻的节点
- Swap Nodes in Pairs 交换LinkList的相邻节点
- 交换单链表中相邻的两个元素 Swap Nodes in Pairs
- (LeetCode)Swap Nodes in Pairs --- 交换两个相邻的结点
- LeetCode 24. Swap Nodes in Pairs(交换节点)
- Swap Nodes in Pairs(交换链表相邻的两个结点)
- [LeetCode]Swap Nodes in Pairs 交换节点
- [C++]LeetCode: 109 Swap Nodes in Pairs (交换相邻节点位置)
- 24. Swap Nodes in Pairs-节点交换|指针
- LeetCode 24. Swap Nodes in Pairs ***** 双节点交换
- LeetCode OJ 之 Swap Nodes in Pairs (交换成对相邻结点的值)
- LeetCode Swap Nodes in Pairs交换链表中的两个节点
- Swap Nodes in Pairs 链表两两交换节点@LeetCode
- 【链表&交换邻节点】Swap Nodes in Pairs
- LeetCode-24 Swap Nodes in Pairs(链表中按要求交换节点)
- LeetCode24 Swap Nodes in Pairs 成对交换链表节点
- [LeetCode] Swap Nodes in Pairs(成对交换节点)
- HIVE 处理非等值连接(join.... on .... like ....)
- 大数据的应用——用户画像
- 0 1背包
- HDU4791【杂】
- text/html & text/plain的区别
- 24. Swap Nodes in Pairs(每两个相邻节点进行一次交换)
- Solr-----1、Solr部署
- 学习过程中的一些总结
- RedTiger's Hackit Level 2
- [UC/OS-II原理及应用]μC/OS-Ⅱ可裁剪性的实现
- 五猴分桃
- 【STM8学习笔记】——IAR For STM8环境下建立工程
- Android 为Recyclerview添加滚动监听事件,以及为它每一个item设置点击监听事件
- 多指标综合评价方法汇总