leetcode143-Reorder List

题目

给定一个单链表 L 的头节点 head ,单链表 L 表示为:
L0 → L1 → … → Ln - 1 → Ln
请将其重新排列后变为:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:
输入:head = [1,2,3,4]
输出:[1,4,2,3]

分析

这道题目的思路其实很明确,先把链表一分为二,再求第二个链表的翻转链表,再把俩个链表相互插入式的连接到一起即可。特别要注意处理一些边界情况,否则很容易空指针

public class LinkNode {
	int val;
	LinkNode next;

	public LinkNode(int data) {
		this.val = data;
		this.next = null;
	}
}
public class LinkList {
	LinkNode head;
	public LinkList() {
		this.head = null;
	}
	public LinkNode getHead() {
		return this.head;
	}
	//添加元素
	public void addNode(int data) {
		LinkNode node = new LinkNode(data);
		if (this.head == null) {
			this.head = node;
		} else {
			LinkNode cur = this.head;
			while(cur.next != null) {
				cur = cur.next;
			}
			cur.next = node;
		}
	}
	//正序打印
	public void print(LinkNode node) {
		while(node != null) {
			System.out.print(node.val);
			System.out.print(" ");
			node = node.next;
		}
		System.out.println();
	}
	public void insert() {
		if(this.head == null) {
			return;
		}
		int cnt = 0;
		LinkNode p = this.head;
		while(p != null) {
			cnt++;
			p = p.next;
		}
		cnt = cnt / 2;
		p = this.head;
		LinkNode tmpPre = null;
		while(cnt > 0) {
			cnt--;
			tmpPre = p;
			p = p.next;
		}
		tmpPre.next = null;
		LinkNode pre = null;
		while(p != null) {
			LinkNode next = p.next;
			p.next = pre;
			pre = p;
			p = next;
		}
		LinkNode first = this.head;
		LinkNode second = pre;
		while(first != null && second != null) {
			LinkNode secondNext = second.next;
			LinkNode firstNext = first.next;
			if(firstNext == null) {
				first.next = second;
				break;
			}
			second.next = firstNext;
			first.next = second;
			if(firstNext == null) {
				first.next = second;
				break;
			}
			first = firstNext;
			second = secondNext;
			if(first.next == null) {
				first.next = second;
				break;
			}
		}
		print(this.head);
	}

}

public class reorderList {
	public static void main(String[] args) {
		LinkList list = new LinkList();
		list.addNode(1);
		list.addNode(2);
		list.addNode(3);
		list.insert();
	}
}

相关推荐

  1. leetcode143-Reorder List

    2024-05-25 20:22:55       30 阅读
  2. LeetCode 141

    2024-05-25 20:22:55       58 阅读
  3. leetcode 153

    2024-05-25 20:22:55       51 阅读
  4. LeetCode--14

    2024-05-25 20:22:55       39 阅读
  5. LeetCode-134】加油站(贪心)

    2024-05-25 20:22:55       56 阅读
  6. leetcode 134.加油站

    2024-05-25 20:22:55       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-25 20:22:55       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-25 20:22:55       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-25 20:22:55       87 阅读
  4. Python语言-面向对象

    2024-05-25 20:22:55       96 阅读

热门阅读

  1. 实现C++ List,双端链表和静态链表

    2024-05-25 20:22:55       30 阅读
  2. Python和Plotly绘制3D图形的小说明

    2024-05-25 20:22:55       37 阅读
  3. kindeditor 上传中 网络图片 去掉 图片空间

    2024-05-25 20:22:55       29 阅读
  4. 中医理疗元宇宙 中医理疗元宇宙

    2024-05-25 20:22:55       29 阅读
  5. SAP OBYC自动记账 详解

    2024-05-25 20:22:55       32 阅读
  6. 一分钟学习LRU和LFU

    2024-05-25 20:22:55       28 阅读
  7. jeb调试发现只能找到sh和ps两个进程

    2024-05-25 20:22:55       30 阅读
  8. uniapp实现下拉过滤查询列表

    2024-05-25 20:22:55       29 阅读