题目: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example, and x = 3, . 先创建两个链表,分别存比x小的链表和比x大的链表,最后吧两个链表链接起来。 /** * 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 *head1,*head2,*p1,*p2; head1=new ListNode(0); head2=new ListNode(0); p1=head1;
Read full article from leetcode:Partition List (链表处理)【面试算法题】 - Havenoidea 的专栏 - 博客频道 - CSDN.NET
No comments:
Post a Comment