• 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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
  • 相关阅读:
    C++项目案例圆和点的关系 (涉及知识点:头文件定义类,cpp文件实现类,类和作用域,linux编译运行c++项目)
    java 3年经验面试题
    洛谷P4956 [COCI2017-2018#6] Davor
    浅谈Java语法中的字符串:String
    Spring Security 如何实现身份认证和授权?
    TinyKv Project2 PartA RaftKV
    DDIM代码详细解读(3):核心采样代码、超分辨率重建
    示例:python环境下字典和列表重组的一个小例子
    【Spring】bean的生命周期
    针对垃圾渗滤液中膜产水脱氮工艺的设计,除氨氮树脂
  • 原文地址:https://blog.csdn.net/wellwang1993/article/details/139044052