[LeetCode]89. Partition List链表划分
2015-11-16 19:50
393 查看
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,
Given
return
Subscribe to see which companies asked this question
解法1:从头往后,找出第一个值大于等于x的节点,然后在这个节点后面找出第一个值小于x的节点,最后将这个小的节点插在大的节点的前面;然后指针移动到上一步的第一个大节点处,继续循环进行上一步处理,直至后面没有值小于x的节点。
实际上外层while循环里的第一个while循环可以拿到外层while循环外面,因为后面每次找到的第一个值大于等于x的节点就是curr本身了。
解法2:另一种方法就是遍历一遍链表,分别将值小于x和值大于等于x的节点分为两个链表链接起来,然后再将后者链接在前者的末尾就可以了。
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given
1->4->3->2->5->2and x = 3,
return
1->2->2->4->3->5.
Subscribe to see which companies asked this question
解法1:从头往后,找出第一个值大于等于x的节点,然后在这个节点后面找出第一个值小于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) { if (head == NULL || head->next == NULL) return head; ListNode *help = new ListNode(0); help->next = head; ListNode* prev = help, *curr = prev->next; while (curr != NULL) { while (curr != NULL && curr->val < x) { // 找出第一个大于等于x的节点 curr = curr->next; prev = prev->next; } if (curr == NULL) break; ListNode *next = curr->next, *temp = curr; // 注意不是从第一个节点开始 while (next != NULL && next->val >= x) { // 找出第一个小于x的节点 next = next->next; temp = temp->next; } if (next == NULL) break; ListNode* tmp = next->next; next->next = prev->next; // 将小于x的节点插在大于等于x的节点之前 prev->next = next; temp->next = tmp; prev = next; // 前移到下一个节点 curr = prev->next; } return help->next; } };
实际上外层while循环里的第一个while循环可以拿到外层while循环外面,因为后面每次找到的第一个值大于等于x的节点就是curr本身了。
/** * 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) { if (head == NULL || head->next == NULL) return head; ListNode *help = new ListNode(0); help->next = head; ListNode* prev = help; while (prev->next != NULL && prev->next->val < x) prev = prev->next; ListNode* curr = prev; while (curr->next != NULL) { if (curr->next != NULL && curr->next->val >= x) curr = curr->next; else { ListNode* tmp = curr->next; curr->next = tmp->next; tmp->next = prev->next; prev->next = tmp; prev = prev->next; } } return help->next; } };
解法2:另一种方法就是遍历一遍链表,分别将值小于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 = new ListNode(0); ListNode* head2 = new ListNode(0); ListNode *next1 = head1, *next2 = head2, *curr = head; while (curr != NULL) { if (curr->val < x) { next1->next = curr; next1 = next1->next; curr = curr->next; next1->next = NULL; } else { next2->next = curr; next2 = next2->next; curr = curr->next; next2->next = NULL; } } next1->next = head2->next; return head1->next; } };
相关文章推荐
- c/c++内存释放知识总结
- ViewPager和视差动画
- 虚调用及其调用的具体形式
- 深入理解BootStrap Item12 -- 标签(label)与徽章(badge)
- 搞ACM的你伤不起(转)
- Java多线程安全问题
- 虚调用及其调用的具体形式
- TextView属性的静态使用与动态使用
- 使用Email发送功能
- PTAM图像接口需要灰度图像
- 武汉大学编译原理第4次作业
- ubuntu12.04连接winscp服务器时出现了:拒绝了SFTP连接,但它监听FTP连接。 想要用FTP协议来代替SFTP吗?最好是用加密的。
- CoreData 数组越界
- 安卓高级xml布局(一)高级输入框EditText设计
- c++利用链表写一个简单的栈
- DoIt is Being Flooded
- Object-c 中Extension解析
- javaSE(01)
- windows IIS安装memadmin
- 本地代码导入github仓库