leetcode: Partition List
2013-05-02 23:58
369 查看
/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function if (head == NULL) return NULL; ListNode *pCur; ListNode *pFront; ListNode *pRear; ListNode *pNewHead = NULL; ListNode *pRearHead = NULL; pCur = head; pFront = NULL; pRear = NULL; while (pCur != NULL) { if (pCur->val < x) { if (pFront == NULL) { pFront = pCur; pNewHead = pCur; } else { pFront->next = pCur; pFront = pFront->next; } } else { if (pRear == NULL) { pRear = pCur; pRearHead = pCur; } else { pRear->next = pCur; pRear = pRear->next; } } pCur = pCur->next; } if (pFront) { pFront->next = pRearHead; if (pRear) pRear->next = NULL; } else if (pRearHead) { pNewHead = pRearHead; } return pNewHead; } };
相关文章推荐
- leetcode-Partition List-86
- [LeetCode]Partition List
- leetCode(10):Partition List 分类: leetCode 2015-06-18 09:08 105人阅读 评论(0) 收藏
- LeetCode:Partition List
- 086 Partition List [Leetcode]
- [Leetcode] Partition List
- 【leetcode】Partition List
- LeetCode 66 Partition List
- Leetcode_Partition List
- leetcode -- Partition List
- LeetCode 86 Partition List
- leetcode 链表 Partition List
- [Leetcode]Partition List
- LeetCode 之 Partition List — C++ 实现
- LeetCode 86: Partition List
- LeetCode - Partition List
- 【leetcode】Partition List(middle)
- [C语言][LeetCode][86]Partition List
- leetCode(10):Partition List
- LeetCode-Partition List