头条面试--单链表操作

头条面试--单链表操作

题目

有一个链表,奇数位是升序,偶数位是降序,现在需要对其进行全部为升序,要求时间复杂度O(n)

www.zeeklog.com  - 头条面试--单链表操作

这里只是为了演示,所以只是列举了几个,真是案例中肯定不是几个数据几点的链表,

思路

  1. 把链表按照奇数位和偶数位进行拆分,拆分成两个链表,
  2. 然后对偶数位的链表进行反转
  3. 然后合并两个列表

www.zeeklog.com  - 头条面试--单链表操作

java代码实现

/** * @auther: lawt * @date: 2018/11/4 08 * @Description: 结点信息 */ public class Node { /** * 为了方便,这两个变量都使用public,而不用private就不需要编写get、set方法了。 * 存放数据的变量,简单点,直接为int型 */ public int data; /** * 存放结点的变量,默认为null */ public Node next; /** * 构造方法,在构造时就能够给data赋值 */ public Node(int data) { this.data = data; } } /** * @auther: lawt * @date: 2018/11/6 10 * @Description: 链表的奇数位升序,偶数位降序,对链表进行排序 * 1 7 2 6 3 5 4 * 时间复杂度是O(n) * 分成三步 * 1:按照奇数位和偶数位进行拆分成两个链表 * 2:对偶数位链表进行反转 * 3:合并两个链表 */ public class Interview1 { /** * 按照奇数位和偶数位进行拆分成两个链表 */ public static Node[] getList(Node head) { //定义两个链表头结点 Node head1 = null; Node head2 = null; //两个指针 Node cur1 = null; Node cur2 = null; //计数 int count = 1; while (head != null) { if (count % 2 == 1) { //奇数位 if (cur1 == null) { cur1 = head; head1 = cur1; } else { cur1.next = head; cur1 = cur1.next; } } else { //偶数位 if (cur2 == null) { cur2 = head; head2 = cur2; } else { cur2.next = head; cur2 = cur2.next; } } head = head.next; count++; } cur1.next = null; cur2.next = null; return new Node[]{head1, head2}; } /** * 对链表进行反转 */ public static Node reversal(Node head) { if (head == null || head.next == null) { return head; } Node prev = null; Node current = head; while (current != null) { Node next = current.next; current.next = prev; prev = current; current = next; } return prev; } /** * 合并两个有序链表 */ public static Node mergeTwoList(Node head1, Node head2) { //递归结束条件 if (head1 == null && head2 == null) { return null; } if (head1 == null) { return head2; } if (head2 == null) { return head1; } //合并后的链表 Node head = null; if (head1.data > head2.data) { //把head较小的结点给头结点 head = head2; //继续递归head2 head.next = mergeTwoList(head1, head2.next); } else { head = head1; head.next = mergeTwoList(head1.next, head2); } return head; } public static void main(String[] args) { Node head = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); Node node6 = new Node(6); Node node7 = new Node(7); head.next = node7; node7.next = node2; node2.next = node6; node6.next = node3; node3.next = node5; node5.next=node4; Node[] nodes = getList(head); Node node = reversal(nodes[1]); Node m = mergeTwoList(nodes[0], node); while (m != null) { System.out.print(m.data + " "); m = m.next; } } }

运行结果

www.zeeklog.com  - 头条面试--单链表操作

Read more

印度统治阶级锁死底层人的5大阳谋

印度统治阶级锁死底层人的5大阳谋

基于社会学和心理学视角: 1. 情感道德: 统治阶级通过塑造道德规范和情感价值观,引导底层人群的行为。例如,宣扬“勤劳致富”“忍耐美德”等观念,让底层人接受现状并自我约束。这种道德框架往往掩盖结构性不平等,使人们将个人困境归咎于自身而非系统。 2. 欲望控制: 通过消费主义和媒体宣传,统治阶级刺激底层人的物质与社会欲望(如名牌、地位),但同时设置经济壁垒,使这些欲望难以实现。底层人被困在追求“更好生活”的循环中,精力被分散,无法聚焦于挑战权力结构。 3. 情绪煽动: 利用恐惧、愤怒或民族主义等情绪,统治阶级可以通过媒体或公共事件转移底层人对社会问题的注意力。例如,制造外部敌人或内部对立(如阶层、种族矛盾),让底层人内耗而非联合反抗。 4. 暴利诱惑: 通过展示少数“成功案例”或快速致富的机会(如赌博、投机),诱导底层人追逐短期暴利。这种机制不仅让底层人陷入经济风险,还强化了对现有经济体系的依赖,削弱长期变革的可能性。 5. 权力震撼: 通过展示统治阶级的权力(

By Ne0inhk