96. 链表划分(partition-list)(c++)----lintcode面试题之链表

(一)题目要求:

给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。

你应该保留两部分内链表节点原有的相对顺序。

(二)示例:

给定链表 1->4->3->2->5->2->null,并且 x=3

返回 1->2->2->4->3->5->null

(三)题解:

方法一:

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */

//方法一:
//(1)遍历链表,将值大于给定值得结点插入新链表p_large
//(2)将p_large与原链表连接

class Solution {
public:
    /*
     * @param head: The first node of linked list
     * @param x: An integer
     * @return: A ListNode
     */
    ListNode * partition(ListNode * head, int x) {
        // write your code here
		//原链表指针
        ListNode *p_current = head,*p_prev = NULL;
		//新链表头指针和尾指针
        ListNode *p_large = NULL,*p_end = NULL;
        
        while(p_current)
        {
            if(p_current->val>=x)
            {
              //结点前结点指向下一个结点(需单独处理头结点)
			  if(p_current == head)
              {
                head = p_current->next;
               // p_prev = p_current->next;
              }
              else
                p_prev->next = p_current->next;
              
			  //将结点插入新链表(需单独处理头结点)
              if(!p_large)
              {
                p_large = p_current;
                p_end = p_current;
              }
              else
              {
                  p_end->next = p_current;
                  p_end = p_current;
              }
              //当前结点后移
              p_current = p_current->next;
            }
			//小于x的结点保留
            else
            {
              p_prev = p_current;
              p_current = p_current->next;
            }
        }
        //将p_large与原链表连接
        if(p_large)
        {
            if(!head)
              head = p_large;
            else
              p_prev->next = p_large;
            //新链表尾结点指向NULL  
            p_end->next = NULL;
        }
        
        return head;
       
    }
};

方法二:

//方法二:
//创建两个链表leftDummy rightDummy 分别保存小于x的结点和大于等于x的结点
//连接两个链表
//注意依旧引入了头结点,避免单独处理头结点

class Solution {
public:
    /**
     * @param head: The first node of linked list.
     * @param x: an integer
     * @return: a ListNode 
     */
    ListNode *partition(ListNode *head, int x) {
        ListNode *leftDummy = new ListNode(0);
        ListNode *rightDummy = new ListNode(0);
        ListNode *leftTail = leftDummy;
        ListNode *rightTail = rightDummy;
        
        while (head != NULL) {
            if (head->val < x) {
                leftTail->next = head;
                leftTail = head;
            } else {
                rightTail->next = head;
                rightTail = head;
            }
            head = head->next;
        }
        //连接两个链表,尾结点指向NULL
        leftTail->next = rightDummy->next;
        rightTail->next = NULL;
        
        return leftDummy->next;
    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值