当前位置:   article > 正文

代码随想录算法训练营day4

代码随想录算法训练营day4

代码随想录算法训练营day4

接着链表。

24.两两交换链表中的节点leetcode24 Swap Nodes in Pairs

class Solution {
   
public:
    ListNode* swapPairs(ListNode* head) {
   
     ListNode*dummyhead=new ListNode(0);
     dummyhead->next=head;
     ListNode*cur=dummyhead;
     while(cur->next!=nullptr&&cur->next->next!=nullptr)
     {
   
         ListNode*tmp=cur->next;
         ListNode*tmp2=cur->next->next->next;
         cur->next= cur->next->next;
         cur->next->next=tmp;
         cur->next->next->next=tmp2;
         cur=cur->next->next;<
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号