两个单链表生成相加链表

本文介绍了一种链表加法算法,通过先反转两个输入链表,再逐位相加并处理进位,最后返回新的链表。文章包含完整的Java代码实现,包括链表节点定义、链表创建及加法运算的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class AddList02 {

	public static Node addList01(Node head1,Node head2)
	{
		head1=reverseList(head1);
		head2=reverseList(head2);
		
		int n1=0;
		int n2=0;
		int n=0;
		int ca=0;	//进位
		
		Node node=null;
		Node pnode=null;
		while(head1!=null||head2!=null)
		{
			n1=head1==null?0:head1.data;
			n2=head2==null?0:head2.data;
			head1=head1.next;
			head2=head2.next;
			n=n1+n2+ca;
			node=new Node(n%10);
			node.next=pnode;
			pnode=node;
			ca=n/10;
		}
		
		if(n>=10)
		{
			node=new Node(n/10);
			node.next=pnode;
		}
		
		return node;
	}
	//实现链表的逆序
	public static Node reverseList(Node head)
	{
		Node pre=null;
		Node next=null;
		while(head!=null)
		{
			next=head.next;
			head.next=pre;
			pre=head;
			head=next;
		}
		return pre;
	}
}




public class CreateQueue {

	Node a=new Node(9);
	Node b=new Node(7);
	Node c=new Node(3);
	Node d=new Node(4);
	Node e=new Node(3);
	Node f=new Node(2);
	Node g=new Node(1);
	
	public CreateQueue()
	{
		a.next=b;
		b.next=c;
		c.next=d;
		d.next=e;
		e.next=f;
		f.next=g;
		g.next=null;
	}
}


public class CreateQueue02 {

	Node a=new Node(1);
	Node b=new Node(2);
	Node c=new Node(3);
	Node d=new Node(4);
	Node e=new Node(3);
	Node f=new Node(2);
	Node g=new Node(9);
	
	public CreateQueue02()
	{
		a.next=b;
		b.next=c;
		c.next=d;
		d.next=e;
		e.next=f;
		f.next=g;
		g.next=null;
	}
}

public class Test {


	public static void main(String[] args) {
	
	CreateQueue cq=new CreateQueue();
	CreateQueue02 cq2=new CreateQueue02();


		
	
		
		Node cur=cq.a;
		System.out.println("之前:");
		while(cur!=null)
		{
			System.out.print(cur.data+" ");
			cur=cur.next;
		}
		System.out.println();
		Node cur2=cq2.a;
		
		while(cur2!=null)
		{
			System.out.print(cur2.data+" ");
			cur2=cur2.next;
		}


		Node head=AddList02.addList01(cq.a, cq2.a);
		
		
		System.out.println();
		System.out.println("之后:");
	
		
			cur=head;
		while(cur!=null)
		{
			System.out.print(cur.data+" ");
			cur=cur.next;
		}
	}
	
	


}




public class Node {

	public int data;
	public Node next;
	public Node(int data)
	{
		this.data=data;
	}
}



(1)在Java中,将单链表按基准划分通常涉及到双指针法。例如,可以定义两个指针,一个快指针先走一步,另一个慢指针每次走一步,当快指针到达基准点时,慢指针的位置就是分界线。然后分别将链表前半部分和后半部分连接起来。以下是简单的示例: ```java public class ListNode { int val; ListNode next; // constructor and getters/setters... } ListNode partition(ListNode head, int k) { if (head == null || head.next == null || k <= 0) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode slow = dummy; ListNode fast = dummy.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } ListNode pivot = slow.next; // 分割点 slow.next = null; // 断开后半部分 ListNode left = dummy.next; ListNode right = pivot; return merge(left, right); // 合并左右两部分 } ListNode merge(ListNode l1, ListNode l2) { // ... 实现合并操作 } ``` (2)合并两个单链表可以通过逐节点比较它们的值,将较小的节点添到结果链表,直到其中一个链表遍历完。然后将剩余的链表接上。伪代码如下: ```java ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) return l2; if (l2 == null) return l1; if (l1.val < l2.val) { l1.next = mergeTwoLists(l1.next, l2); return l1; } else { l2.next = mergeTwoLists(l1, l2.next); return l2; } } ``` (3)集合运算如并集、交集和差集可以借助哈希表来提高效率。首先将每个链表转换成哈希集合,然后进行计算。这里给出简化的描述: - 并集:取两个集合的元素集合 - 交集:找出同时存在于两个集合中的元素 - 差集:在第一个集合中减去第二个集合的元素 ```java HashSet<Integer> set1 = toHashSet(list1); HashSet<Integer> set2 = toHashSet(list2); // 并集:set1.addAll(set2) // 交集:set1.retainAll(set2) // 差集:set1.removeAll(set2) ``` 其中`toHashSet()`函数用于将链表转换为哈希集合。 (4)两个多项式相加,需要对对应系数进行累,并考虑进位。可以定义一个节点类存储系数和指数,然后逐项相加。示例代码: ```java class Polynomial { List<Pair<Integer, Integer>> terms; // ... 添相加等方法 } Pair<Integer, Integer> addTerm(Pair<Integer, Integer> a, Pair<Integer, Integer> b) { int sumCoef = a.getKey() + b.getKey(); int maxExponent = Math.max(a.getValue(), b.getValue()); return new Pair<>(sumCoef, maxExponent); } Polynomial add(Polynomial p1, Polynomial p2) { // ... 实现合并两个多项式的操作 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值