leetcode21-Merge Two Sorted Lists

题目

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]
示例 2:
输入:l1 = [], l2 = []
输出:[]
示例 3:
输入:l1 = [], l2 = [0]
输出:[0]

分析

用一个指针去串联俩个链表,用一个指针记录新链表的头结点

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 LinkNode merget(LinkNode nodea,LinkNode nodeb) {
		LinkNode head = new LinkNode(0);
		LinkNode pNode = new LinkNode(0);
		while(nodea != null && nodeb != null) {
			if(nodea.val < nodeb.val) {
				if(head.next==null) {
					head.next = nodea;
				}
				pNode.next = nodea;
				nodea = nodea.next;
				pNode = pNode.next;
			} else {
				if(head.next==null) {
					head.next = nodeb;
				}
				pNode.next = nodeb;
				nodeb = nodeb.next;
				pNode = pNode.next;

			}
		}
		while(nodea != null) {
			if(head.next==null) {
				head.next = nodea;
			}
			pNode.next = nodea;
			nodea = nodea.next;
			pNode = pNode.next;
		}
		while(nodeb != null) {
			if(head.next==null) {
				head.next = nodeb;
			}
			pNode.next = nodeb;
			nodeb = nodeb.next;
			pNode = pNode.next;
		}
		print(head.next);
		return head.next;
	}

}
public class mergeTwoSortedLists {
	public static void main(String[] args) {
		LinkList list1 = new LinkList();
		list1.addNode(1);
		list1.addNode(2);
		list1.addNode(3);
		LinkList list2 = new LinkList();
		list2.addNode(1);
		list2.addNode(3);
		list2.addNode(4);
		list1.merget(list1.getHead(),list2.getHead());
	}

}

相关推荐

  1. LeetCode 21

    2024-05-11 14:02:02       43 阅读
  2. MergeTwoSortedLists 【合并有序链表】

    2024-05-11 14:02:02       67 阅读
  3. LeetCode hot100-21

    2024-05-11 14:02:02       39 阅读
  4. Leetcode 4.21

    2024-05-11 14:02:02       38 阅读
  5. LeetCode 1084, 135, 21

    2024-05-11 14:02:02       40 阅读
  6. Leetcode Sheet】Weekly Practice 21

    2024-05-11 14:02:02       49 阅读
  7. LeetCode--26

    2024-05-11 14:02:02       54 阅读
  8. Leetcode--27

    2024-05-11 14:02:02       56 阅读

最近更新

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

    2024-05-11 14:02:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 14:02:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 14:02:02       87 阅读
  4. Python语言-面向对象

    2024-05-11 14:02:02       96 阅读

热门阅读

  1. 单例模式(Singleton Pattern)

    2024-05-11 14:02:02       38 阅读
  2. Flask-Login 实现用户认证

    2024-05-11 14:02:02       30 阅读
  3. 投影与降维

    2024-05-11 14:02:02       34 阅读
  4. npm入门介绍

    2024-05-11 14:02:02       33 阅读
  5. 关于OpenJDK的学习笔记

    2024-05-11 14:02:02       31 阅读
  6. Python面试题【数据结构和算法部分101-130】

    2024-05-11 14:02:02       32 阅读
  7. 【Qt】QStandardItem更新数据注意事项

    2024-05-11 14:02:02       38 阅读
  8. mysql5.7.44误删除数据后,使用binlog日志恢复

    2024-05-11 14:02:02       37 阅读
  9. UDP socket编程示例

    2024-05-11 14:02:02       36 阅读
  10. 已经声明,但提示“未声明的标识符”

    2024-05-11 14:02:02       33 阅读
  11. 探索前端技术的未来:新兴工具与框架的引领

    2024-05-11 14:02:02       30 阅读
  12. C#[数据结构]之数组

    2024-05-11 14:02:02       33 阅读