leetcode:Swap Nodes in Pairs
来源:互联网 发布:五轴编程哪个软件强 编辑:程序博客网 时间:2024/04/28 17:24
交换两个相邻变量的结点,不能仅仅只是交换他们的值
使用双指针的做法,n1,n2,
n1.next = n2.next;
n2.next = n1;
pre.next = n2;
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode swapPairs(ListNode head) { if (head == null || head.next == null) { return head; } ListNode n1 = head, n2 = null; head = head.next; ListNode pre = null; while (n1 != null && n1.next != null) { n2 = n1.next; if (pre != null) { pre.next = n2; } n1.next = n2.next; n2.next = n1; pre = n1; n1 = pre.next; } return head; }}
0 0
- LeetCode:Swap Nodes in Pairs
- LeetCode: Swap Nodes in Pairs
- [Leetcode] Swap Nodes in Pairs
- LeetCode : Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Leetcode: Swap nodes in pairs
- [LeetCode]Swap Nodes in Pairs
- [LeetCode]Swap Nodes in Pairs
- LeetCode-Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- LeetCode | Swap Nodes in Pairs
- 【leetcode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- 【LeetCode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- Leetcode Swap Nodes in Pairs
- LeetCode Swap Nodes in Pairs
- iOS多任务详解
- 剁椒菜梗
- 可适度规划科是个可还是分开管理化家
- CSS选择器
- 关于row movement的一点应用
- leetcode:Swap Nodes in Pairs
- mac os x下使用命令行安装软件
- Win7中Nexus5找不到驱动
- 减肥运动
- The die is cast - UVa 657 搜索
- oracle事务处理及实例演示jdbc操作批量删除
- 《BOM物料管理》读书笔记
- C# ZedGraph画曲线图形
- 教大家怎样打理长卷发