首页 > 学院 > 开发设计 > 正文

86. Partition List

2019-11-11 05:11:52
字体:
来源:转载
供稿:网友

这题1刷只想到用新的点,2刷试试不用新的点,直接在原链表中运行。 Ps 链表的申请 复习

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* partition(ListNode* head, int x) { ListNode *p = new ListNode(0); ListNode *q = new ListNode(0); ListNode *ph = p; ListNode *qh = q; while(head != NULL){ if(head -> val < x){ p -> next = head; p = p -> next; } else{ q -> next = head; q = q -> next; } head = head -> next; } p -> next = qh -> next; q -> next = NULL; return ph -> next; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表