【LeetCode】成对交换节点

e.g. 给定链表 1->2->3->4,返回 2->1->4->3 的头节点。

我写了个常见的从头节点遍历,少量的奇数个或偶数个数据都能成功重新排列。但链表过长时结果显示 Time Limit Exceeded 。

 1 ListNode* swapPairs(ListNode* head) {
 2     ListNode *p = head, *q, *pre;
 3     while (p) {
 4         if (q = p->next) {
 5             if (p == head)  head = q;
 6             p->next = q->next;
 7             q->next = p;
 8             if (pre)    pre->next = q;
 9         }
10         pre = p;
11         p = p->next;
12     }
13     return head;
14 }

看到有一个答案使用二级指针,忘了这种方法了!

pp 从指向 head 指针到指向所有 next 指针遍历下去,p 指向 *pp 表示指向当前结点,q 指向 p 的下一个节点。

 1 ListNode* swapPairs(ListNode* head) {
 2     ListNode **pp = &head, *p, *q;
 3     while ((p = *pp) && (q = p->next)) {
 4         p->next = q->next;
 5         q->next = p;
 6         *pp = q;
 7         pp = &(p->next);
 8     }
 9     return head;
10 }

看到有些答案标题写使用递归,想了下,把两种思路都 DIY 为递归方法,Accepted了。

 1 ListNode* swapPairs(ListNode* head) {
 2     if (!head || !head->next)  return head;
 3     ListNode *p = head, *q = p->next;
 4     if (!q->next)
 5         p->next = NULL;
 6     else
 7         p->next = swapPairs(q->next);
 8     q->next = p;
 9     return q;
10 }
1 ListNode* swapPairs(ListNode* head) {
2     if (!head || !head->next)    return head;
3     ListNode **pp = &head, *q = (*pp)->next;
4     (*pp)->next = swapPairs(q->next);
5     q->next = *pp;
6     pp = &q;
7     return *pp;
8 }

可以看到二级指针代码量确实会少一点,而且使用递归代替迭代有时能减少时间。

但答案这种递归方法最为简单,只用定义一个指针。改为使用二级指针目测无法实现。

1 ListNode* swapPairs(ListNode* head) {
2     if(!head || !head->next)    return head;
3     ListNode *next = head->next;
4     head->next = swapPairs(next->next);
5     next->next = head;
6     return next;
7 }
时间: 2024-08-05 14:55:04

【LeetCode】成对交换节点的相关文章

Leetcode:Swap Nodes in Pairs 链表成对交换节点

Swap Nodes in Pairs: 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 va

[LeetCode] Swap Nodes in Pairs 成对交换节点

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, onl

[LintCode] Swap Nodes in Pairs 成对交换节点

Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2->3->4, you should return the list as 2->1->4->3. Challenge Your algorithm should use only constant space. You may not modify the values in the lis

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

我要好offer之 链表大总结

单链表是一种递归结构,可以将单链表看作特殊的二叉树(我把它叫做一叉树) 单链表的定义: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ 1. O(1)时间删除结点 ListNode* DeleteNode(ListNode* pHead, ListNode* dele

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 讲一个排序好的数组转换成二叉搜索树,这题没想出来,基本上是参考别人的,边界条件应该注意一下: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * T

[Leetcode] Convert sorted list to binary search tree 将排好的链表转成二叉搜索树

---恢复内容开始--- Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题目要求:转成高度平衡的二叉搜索树. 高度平衡的二叉搜索树:i)左子树和右子树的高度之差的绝对值不超过1; ii)树中的每个左子树和右子树都是AVL树; iii)每个节点都有一个平衡因子(balance factor bf),任一节点的平衡因子是1,0,

[LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to show hints. Hints: If you notice carefully in the flattened tree, each node's right child points