Leetcode 328 Odd even Linked list link list

Source: Internet
Author: User

Given 1->2->3->4->5->NULL ,
Return 1->3->5->2->4->NULL .

is to put the ordinal number in front of the singular, and the number is even in the back

My method is to say an even number of inserts to the end of the list.

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 classSolution {Ten  Public: Onelistnode* Oddevenlist (listnode*head) { A         if(!head)returnhead;//empty linked list return -listnode* last =head; -         intCNT =1; the          for(; last->next; last = Last->next, + +CNT); -         if(CNT <3)returnhead;//linked list length less than 3 return -listnode* now =head; -listnode* end =Last ; +              -          for(inti =0; i< cnt/2; now = Now->next, + +i) { +              Alistnode* next = now->Next; atNow->next = next->Next; -                  -End->next =Next; -Next->next =NULL; -                  -End =Next; in              -         } to         returnhead; +     } -};

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