题解 | #重排链表##递归#

重排链表

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); } } };
全部评论

相关推荐

大叔叔1:你把自己说的话打码,所以你想表达什么
点赞 评论 收藏
分享
zhiyog:哈哈哈,其实是津巴布韦币
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务