這題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; }};新聞熱點
疑難解答