题解 | #重排链表##递归#
重排链表
http://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b
/**
- Definition for singly-linked list.
- struct ListNode {
-
int val;
-
ListNode *next;
-
ListNode(int x) : val(x), next(NULL) {}
- }; */ class Solution { public: void reorderList(ListNode *head) { if(head==NULL) return; else if(head->next==NULL){ return; } else{ ListNode *pfirst = head; ListNode *pmid = pfirst->next; ListNode *plast = pmid->next; while(plast!=NULL){ plast = plast->next; pmid = pmid->next; pfirst = pfirst->next; } pfirst->next = plast; pmid->next = head->next; head->next = pmid; reorderList(pmid->next); } } };