Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
来源:互联网 发布:微店 淘宝客 编辑:程序博客网 时间:2024/05/30 23:03
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. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *swapPairs(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode dummy(0); ListNode *tail = &dummy; ListNode *pre = head; ListNode *cur = NULL; ListNode *nxt = NULL; while(pre != NULL) { cur = pre->next; if(cur != NULL) { nxt = cur->next; cur->next = pre; pre->next = NULL; tail->next = cur; tail = pre; pre = nxt; } else { tail->next = pre; return dummy.next; } } return dummy.next; }};
0 0
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
- [LeetCode]Swap Nodes in Pairs 交换节点
- Swap Nodes in Pairs 链表交换相邻的节点
- Swap Nodes in Pairs 交换LinkList的相邻节点
- [C++]LeetCode: 109 Swap Nodes in Pairs (交换相邻节点位置)
- (LeetCode)Swap Nodes in Pairs --- 交换两个相邻的结点
- 【leetcode】【单链表】【24】Swap Nodes in Pairs
- 【LeetCode】011 Swap Nodes in Pairs 两两换位
- LeetCode-------24. 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
- Uva-1486-Transportation
- 追加写入txt
- 会场安排问题
- oracle错误:1067进程意外终止
- Java 多线程 死锁 Java编程思想读书笔记
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
- Jump Game II
- 黑马程序员_内省和JavaBean笔记
- wegergerhge
- 斯蒂芬二个如果
- 电话地方规划法规和分
- vs2013 Ultimate激活码
- 是豆腐干豆腐干豆腐干豆腐干的
- 的分好风光好风光