24. 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, only nodes itself can be changed.

分析

算法一:

看到题目,首先想到使用双指针,来进行交换,但是交换过后,会存在一个连接断开的问题:
a->b->c->d->e...
交换c,d以后的结果是  a->b  d->c->e
                       |_____|
所以需要三个指针,将交换以后的连接更新一下。
边界条件:
1 开始的两个node,并不需要更新连接
2 node总数可能是奇数,可能是偶数,所以判定的时候需要注意发生 a == NULL 并调用 a->next这种 runtime error的情况

使用三个指针,两个指向节点,一个指向某个节点中的next,所以应该是ListNode **,用来连接交换后的新的链路
  1. class Solution {
  2. public:
  3. ListNode* swapPairs(ListNode* head) {
  4. if(head == NULL) return NULL;
  5. ListNode **pp = & head, *a, *b;
  6. while((a = *pp) && (b = a->next)){
  7. a->next = b->next;
  8. b->next = a;
  9. *pp = b;
  10. pp = &(a->next);
  11. }
  12. return head;
  13. }
  14. };

算法二:

使用递归算法,使用两个指针指向当前节点和下一个,并对剩下的 list 使用swapPairs
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* swapPairs(ListNode* head) {
  12. if(head == NULL || head->next == NULL) return head;
  13. ListNode * a = head, *b = head->next,*tmp;
  14. tmp = b->next;
  15. b->next = a;
  16. a->next = swapPairs(tmp);
  17. return b;
  18. }
  19. };









原文地址:https://www.cnblogs.com/zhxshseu/p/6282497.html