Leetcode 328 Odd even Linked list (odd-even linked list) (*)

Source: Internet
Author: User

translation
Given a single-linked list, all of the odd nodes are grouped and the even nodes are immediately followed. Note that we are talking about the singular node instead of the value on the node. You should try to do it in place. The program should be in O (1) space complexity and O (nodes) time complexity are completed. For example: given1 -2 -3 -4 -5 -NULLReturn1 -3 -5 -2 -4 -NULL。 Note: The relative position of the odd and even nodes in the final collation should be the same as the input. The first node is an odd node, the second node is a even node, and so on ...
Original
GivenaSingly linked list, group all odd nodes together followed by  theEven nodes. Please note here we is talking about theNode Number  and  not  the value inch  theNodes. You shouldTry  to  Do it inchPlace. The program should runinchO (1)SpaceComplexity andO (nodes) TimeComplexity. Example:given1-2-3-4-5-NULL,return 1-3-5-2-4-NULL. Note:therelativeOrder inside both theEven andOdd groups should remain as itWasinch  theInput. The Firstnode is considered odd, the SecondNode even andSoOn   ...
Analysis

The first problem of winter vacation, rural home really super cold ... I think the idea may not be too concise, coupled with the consideration of not comprehensive, resulting in continuous error, debugging, error, debugging ... It took a long time to finish, and the code was no longer concise.

ListNode*Oddevenlist (ListNode*Head) {if(!Head|| !Head -NextreturnHead BOOL Oddflag= true; ListNode*Odd=Head ListNode*Even=Head -Next ListNode*Oddhead=Odd ListNode*Evenhead=even; Head=Head -Next -Next while(head) {if(Oddflag) {Odd -Next=Head Odd=Odd -Next Oddflag= false; }Else{Even -Next=Head Even=Even -Next Oddflag= true; } head=Head -Next }if(!Oddflag) Even -Next= NULL; Odd -Next=Evenhead;returnOddhead;}

The process of Dalat is probably like this:

1,判断是否为空,两种情况都返回head。2,新建odd和even来不断遍历,和head一样,它们三个是会移动的。3,oddHead和evenHead是不会动的,用于最后拼凑新的链表。4,用isOdd来判断当前的节点是否是奇节点,不管是不是,它们的操作都是类似的。5,最后的时候,如果是奇节点结尾的,那么可能偶节点最后一个会是多余的,所以需要将它干掉。6,拼凑新的链表用于返回。

But think about it, there is still a lot of room for improvement, such as:

Defining the Oddhead and defining the Evenhead, there is a good solution: use head as the Oddhead. So that when we return we can go straight back to the head, so the new question is coming? Before we used the head to iterate, now what? We need to find a new solution.

Let's review the previous algorithm: Use head to traverse each node and use a BOOL variable to flag the nature of the node (odd or even). As an example:

1,2,3,4,5,NULL

We find that the next node of the odd node happens to be the next node of the even node (the next node of 1 should be the next node of 2, that is, 3), and the next node of the even node is exactly the next node of the odd node. This allows us to traverse through only the odd and even two variables, eliminating the head to traverse, and omitting a variable oddhead, as well as directly returning to head, why not?

Do you remember the way we used to take such a step?

if (!isOdd)    even->nextNULL;odd->next

If you follow the current line of thought, then you do not need this judgment at all, because in the process of seeking next, you have set the null place to NULL.

So the code changes to:

ListNode* Oddevenlist (ListNode* head) {if(!head)returnHeadListNode* odd = head;ListNode* even = head->Next;ListNode* Evenhead = even; while(odd->Next&& even->Next) {odd->Next= even->Next; Odd = odd->Next; Even->Next= odd->Next; even = even->Next; } odd->Next= Evenhead;returnHead;}

The order of the parts in the middle of the while loop cannot be reversed, and the Odd->next is set to Even->next only after the odd has been updated.

Code
/*** Definition forsingly-linked list.* struct ListNode {*intval;* ListNode *Next; * ListNode (intx): Val (x),Next(NULL) {}* };*/classSolution { Public: listnode* oddevenlist (listnode* head) {if(!head) return head;        listnode* odd = head; listnode* even = head->Next; listnode* evenhead = even; while(odd->Next&& even->Next) {odd->Next= even->Next; Odd = odd->Next; Even->Next= odd->Next; even = even->Next; } odd->Next= Evenhead;    return head; }};

Leetcode 328 Odd even Linked list (odd-even 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.