86.Partition List

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 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

思路:此题的任务是划分链表,比x小的链表节点都出现在比x大或者等于的节点前面,而且还要保持原来的相对位置不要发生变化。我们新建两个链表了l1,l2,遍历原来的链表 ,把值小于x的节点都加入l1,把值大于或者等于x的节点都加入l2,最后将l2连接至l1的后面,所得的链表就是我们所求的结果了。

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* partition(ListNode* head, int x) {
  12. ListNode *head1,*head2;
  13. head1=head2=NULL;
  14. ListNode *cur1,*cur2;
  15. cur1=cur2=NULL;
  16. while(head){
  17. ListNode *cur=head;
  18. head=head->next;
  19. cur->next=NULL;
  20. if(cur->val<x){
  21. if(!head1){
  22. head1=cur;
  23. cur1=head1;
  24. }
  25. else{
  26. cur1->next=cur;
  27. cur1=cur1->next;
  28. }
  29. }else{
  30. if(!head2){
  31. head2=cur;
  32. cur2=head2;
  33. }else{
  34. cur2->next=cur;
  35. cur2=cur2->next;
  36. }
  37. }
  38. }
  39. if(!cur1)
  40. return head2;
  41. cur1->next=head2;
  42. return head1;
  43. }
  44. };

来自为知笔记(Wiz)

时间: 2024-10-22 13:42:05

86.Partition List的相关文章

LeetCode --- 86. Partition List

题目链接:Partition List 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,

[leedcode 86] Partition List

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 1->4->3->2

[LeetCode] 86. Partition List 解题思路

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 1->4->3->2

86. Partition List (List)

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 1->4->3->

86. Partition List java solutions

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 1->4->3->2

[LeedCode OJ]#86 Partition List

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/partition-list/ 题意: 给定一个链表和一个x,要求在不改变其在原本链表中相对位置的情况下,将小于x的结点放在新链表的左边,大于等于x的结点放在新链表的右边 思路: 思路很简单,新建两个链表,一个存放大于等于x的结点,一个存放小于x的结点,左后再合并两个链表即可. /** * Definition for s

leetCode 86.Partition List(分区链表) 解题思路和方法

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 1->4->3->

【一天一道LeetCode】#86. Partition List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 origin

LeetCode OJ 86. Partition List

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 1->4->3->2