题解 | #重排链表#

重排链表

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; vector<ListNode *> tmp; ListNode *node=head; while(node!=NULL){ tmp.push_back(node); node=(*node).next; } node=head; for(auto i=tmp.end()-1;true;--i){ if((*i)==(*node).next || (*i)==(node)){ (**i).next=NULL; break; } (**i).next=(*node).next; (*node).next=*i; node=(**i).next; } } };
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务