[Bug] Two linked list, staggered to another linked list

Source: Internet
Author: User

New ListNode ( -1= dummy3;  while (head2!=null&&head1!=null) {     Pre.next=head1;      = Head1.next;     Head1.next=head2;      = Head2.next;     Head2.next=null;     Pre=head2;     Head1=head1next;     Head2=head2next;}

The code above is the correct notation.

1ListNode dummy3 =NewListNode (-1);2ListNode pre =Dummy3;3  while(head2!=NULL&&head1!=NULL){4pre.next=Head1;5Pre.next.next =head2;6      //ListNode head1next = head1.next;7      //head1.next=head2;8      //ListNode head2next = head2.next;9      //Head2.next=null;TenPre=head2; Onehead1=Head.next; AHead2=Head.next; -}

The above code was wrong, and after the execution of the 5th line of code, the Head1 's next pointer was actually lost, because the 5th line of code was actually head1 the next pointer was redefined, pointing to head2, but before changing the pointer, Its original next pointer has not been modified, and the next pointer will be the new head1.

Beware of it!

[Bug] Two linked list, staggered to another linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.