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