86. Partition List

来源:互联网 发布:it售后工程师岗位职责 编辑:程序博客网 时间:2024/06/02 09:20

这题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;    }};
0 0
原创粉丝点击