双指针-重排链表

1.题目描述

题目链接
双指针-重排链表_第1张图片
解题思路:
①使用快慢指针找到链表的中心位置。
②逆置后半段的链表。
③使用头插法重排链表
双指针-重排链表_第2张图片
之所以这里强调结点2指向结点3是因为和后面头插法的判断语句有关,不然容易出错。

2.测试代码

    void reorderList(ListNode* head) {
        ListNode* slow;//定义一个慢指针
        slow=head;
        ListNode* fast;//定义一个快指针
        fast=head;
        while(fast!=nullptr&&fast->next!=nullptr){
            slow=slow->next;
            fast=fast->next->next;
        }
        ListNode* u;
        u=head;//u指向头结点
        ListNode* s;
        ListNode* t;
        s=slow->next;
        slow->next=nullptr;
        while(s!=nullptr){//对后半链表进行逆置的操作
            t=s->next;
            s->next=slow;
            slow=s;
            s=t;
        }
        ListNode* m;
        ListNode* n;
        while(u->next!=nullptr&&slow->next!=nullptr){//进行头插
            m=u->next;
            n=slow->next;
            u->next=slow;
            slow->next=m;
            u=m;
            slow=n;
        }
    } 

3.测试结果

双指针-重排链表_第3张图片
结果分析:通过快慢指针、链表逆置和头插法这三个基本操作使本题的时间复杂度达到了O(n)

4.易错点

        while(u->next!=nullptr&&slow->next!=nullptr){//进行头插
            m=u->next;
            n=slow->next;
            u->next=slow;
            slow->next=m;
            u=m;
            slow=n;
        }

这部分代码如果写成了

        while(u!=nullptr&&slow!=nullptr){//进行头插
            m=u->next;
            n=slow->next;
            u->next=slow;
            slow->next=m;
            u=m;
            slow=n;
        }

会报如下错:

=================================================================
==21==ERROR: AddressSanitizer: heap-use-after-free on address 0x502000000098 at pc 0x55a38fd65091 bp 0x7ffea5903440 sp 0x7ffea5903438
READ of size 8 at 0x502000000098 thread T0
    #2 0x7f4e227cad8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: c289da5071a3399de893d2af81d6a30c62646e1e)
    #3 0x7f4e227cae3f  (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId: c289da5071a3399de893d2af81d6a30c62646e1e)
0x502000000098 is located 8 bytes inside of 16-byte region [0x502000000090,0x5020000000a0)
freed by thread T0 here:
    #3 0x7f4e227cad8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: c289da5071a3399de893d2af81d6a30c62646e1e)
previously allocated by thread T0 here:
    #4 0x7f4e227cad8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: c289da5071a3399de893d2af81d6a30c62646e1e)
Shadow bytes around the buggy address:
  0x501ffffffe00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501ffffffe80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501fffffff00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x501fffffff80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x502000000000: fa fa fd fa fa fa fd fa fa fa fd fd fa fa fd fd
=>0x502000000080: fa fa fd[fd]fa fa fd fd fa fa fd fd fa fa fd fd
  0x502000000100: fa fa fd fd fa fa fd fd fa fa fa fa fa fa fa fa
  0x502000000180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000280: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x502000000300: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==21==ABORTING

通过代码调试和手动模拟发现出现上面的错误是因为链表指针指向了自己

你可能感兴趣的:(算法题,链表,数据结构,c++,c语言)