2.4 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前。

思路:将小于的结点还是保存在原来的链表中,将大于等于x的结点加入一个新的链表,最后将这两个链表链接起来。

C++实现代码:

#include<iostream>
#include<new>
using namespace std;struct ListNode
{int val;ListNode *next;ListNode(int x):val(x),next(NULL) {}
};void createList(ListNode *&L)
{int arr[10]= {1,2,3,2,5,6,7,3,9,1};int i;ListNode *p=NULL;for(i=0; i<10; i++){ListNode *tmp=new ListNode(arr[i]);if(L==NULL){L=tmp;p=tmp;}else{p->next=tmp;p=tmp;}}
}void splitX(ListNode *L,int x)
{if(L==NULL)return;ListNode *pre=L;ListNode *p=L;ListNode *L1=NULL;ListNode *q=NULL;while(p){if(p->val<x){pre=p;p=p->next;}else{ListNode *tmp=p;p=tmp->next;pre->next=p;tmp->next=NULL;if(L1==NULL){L1=tmp;q=tmp;}else{q->next=tmp;q=tmp;}}}pre->next=L1;
}int main()
{ListNode *head=NULL;createList(head);ListNode *p=head;while(p){cout<<p->val<<" ";p=p->next;}cout<<endl;splitX(head,5);p=head;while(p){cout<<p->val<<" ";p=p->next;}cout<<endl;
}

careercup-链表 2.4相关推荐

  1. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  2. [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表

    4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...

  3. M1: 复制带随机指针的链表

    一个链表问题:复制带随机指针的链表 题目:有一个链表L,其每个节点有2个指针,一个指针next指向链表的下个节点,另一个random随机指向链表中的任一个节点,可能是自己或者为空,写一个程序,要求复制 ...

  4. 【CareerCup】 Linked Lists—Q2.5

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/22097191     题目: Given a circular linked list, ...

  5. 伍六七带你学算法 入门篇-链表的中间节点

    力扣-876链表的中间节点 难度-简单 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此 ...

  6. Go 学习笔记(80)— Go 标准库 container/list(单链表、双链表)

    列表是一种非连续存储的容器,由多个节点组成,节点通过一些变量记录彼此之间的关系.列表有多种实现方法,如单链表.双链表等. ​ 在 Go 语言中,将列表使用 container/list 包来实现,内部 ...

  7. 数据结构(08)— 线性单链表基本操作

    1. 线性单链表数据结构 // 假定每个结点的类型用 SNode 表示 typedef struct SNodeTag {int data; // 所存储的数据元素SNodeTag *next; // ...

  8. 数据结构(05)— 线性单链表实战

    1. 设计思路 本项目的实质是完成对考生信息的建立.查找.插入.修改.删除等功能,可以首先定义项目的数据结构,然后将每个功能写成一个函数来完成对数据的操作,最后完成主函数以验证各个函数功能并得出运行结 ...

  9. 使用python建立简单的单链表

    代码 import sysclass ListNode:def __init__(self, x):self.val = xself.next = None# 将列表转换成链表 def list_to ...

  10. LeetCode19. Remove Nth Node From End of List 删除链表中的倒数第n个位置的元素

    前言 本文是LeetCode19. Remove Nth Node From End of List解法,这个题目需要删除链表中的倒数第n个位置的元素 代码 # -*- coding: utf-8 - ...

最新文章

  1. 重磅!Gartner公布2019年十大战略科技发展趋势
  2. Python之pyecharts:利用pyecharts绘制2020年11月16日微博话题热度排行榜实时变化
  3. BUUCTF 新年快乐 内涵的软件 Java逆向解密 刮开有奖
  4. 静态成员变量的初始化,vector类型变量初始化
  5. 查询大于2分钟的数据
  6. python序列符号_初识Python(4)__Python序列
  7. 长微博android,Android 上最强大的长微博工具:BlackLight 长微博
  8. 【JVM】第四章 Java内存模型
  9. python学习笔记8-列表、集合、字典推导式
  10. SpringMVC相关
  11. CFS Scheduler(CFS调度器)
  12. mooon-agent核心设计图
  13. UML入门以及Plant UML工具介绍
  14. Aggregation-Based Graph Convolutional Hashing forUnsupervised Cross-Modal Retrieval
  15. mysql计算年休假天数_如何计算年假多少天
  16. Jetson Nano配置YOLOv5并实现FPS=25
  17. 服务端svn配置及首次本地项目上传svn
  18. Bluetooth 由来及发展历程
  19. 从头撸JavaScript--数组
  20. Android RecyclerView实现图片自适应高度的瀑布流

热门文章

  1. chmod chown
  2. Highlighter与BooleanQuery查询
  3. XRDP与VNC的关系
  4. POJ 3660 Cow Contest (闭包传递)
  5. SQL Server与Oracle对比学习:权限管理(一)
  6. WCF Service Configuration Editor的使用
  7. 使用 ExpandableListView 实现折叠ListView
  8. FZU 1692 Key problem
  9. 《Ray Tracing in One Weekend》——Chapter 8: Metal
  10. 数据挖掘比赛笔记总结