打印两个有序链表的公共部分

本文介绍了一种打印两个有序链表公共元素的方法,并提供了两种实现方式:一种使用数组遍历查找,另一种通过链表节点比较的方式进行。左神提供的代码示例展示了如何用Java实现这些方法。

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

//打印两个有序链表的公共部分
public class getSameList{
  
     //获得两个有序链表的公共部分
    public static void GetSameList(int[] head1,int[] head2)
    {
      if(head1==null||head2==null)
      {
      	return;
      }
      for(int i=0;i<head1.length;i++)
      {
      	  for(int j=0; j<head2.length;j++)
      	  {
             if(head1[i]<head2[j])
             {
             	break;
             }
              if(head1[i]>head2[j])
             {
             	continue;
             }
             System.out.print(head1[i]+" ");


      	  }
      }
      


    }
	public static void main(String []args)
	{
       int []head1={1,3,5,7,9};
       int []head2={2,3,5,6,8};
       GetSameList(head1,head2);
	}
}



左神的代码:

public class Problem_01_PrintCommonPart {
    
	
	//链表节点的定义
	public static class Node {
		public int value;
		public Node next;
		public Node(int data) {
			this.value = data;
		}
	}
   //打印两个链表的公共部分
	public static void printCommonPart(Node head1, Node head2) {
		System.out.print("Common Part: ");
		while (head1 != null && head2 != null) {
			if (head1.value < head2.value) {
				head1 = head1.next;
			} else if (head1.value > head2.value) {
				head2 = head2.next;
			} else {
				System.out.print(head1.value + " ");
				head1 = head1.next;
				head2 = head2.next;
			}
		}
		System.out.println();
	}
    //打印链表内容
	public static void printLinkedList(Node node) {
		System.out.print("Linked List: ");
		while (node != null) {
			System.out.print(node.value + " ");
			node = node.next;
		}
		System.out.println();
	}

	public static void main(String[] args) {
		Node node1 = new Node(2);
		node1.next = new Node(3);
		node1.next.next = new Node(5);
		node1.next.next.next = new Node(6);

		Node node2 = new Node(1);
		node2.next = new Node(2);
		node2.next.next = new Node(5);
		node2.next.next.next = new Node(7);
		node2.next.next.next.next = new Node(8);

		printLinkedList(node1);
		printLinkedList(node2);
		printCommonPart(node1, node2);

	}

}

  

在C语言中,可以使用双指针法来解决这个问题。首先,我们将两个链表的头节点分别指向两个有序链表,并将它们的指针设置为第一个元素。然后我们创建一个新的链表来存储交集。下面是算法步骤: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct ListNode { int val; struct ListNode *next; } ListNode; // 创建新节点函数 ListNode* createNode(int data) { ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->val = data; node->next = NULL; return node; } // 求两个链表交集 ListNode* intersectTwoLists(ListNode* list1, ListNode* list2) { ListNode *slow1 = list1, *fast1 = list1, *slow2 = list2, *fast2 = list2; // 如果list1或list2为空,则直接返回另一个链表的头节点 if (!list1 || !list2) { return list1 ? list2 : list1; } while (fast1 && fast2) { if (fast1->val > fast2->val) { slow1 = slow1->next; // 向前移动较短链表的慢指针 if (!slow1) break; // 如果短链表遍历完,说明无交集 } else if (fast1->val < fast2->val) { slow2 = slow2->next; // 同样向前移动较长链表的慢指针 if (!slow2) break; // 若长链表遍历完,说明无交集 } else { // 否则,找到相同值,添加到结果链表并同时向后移动指针 ListNode *result = createNode(fast1->val); slow1 = slow1->next; fast1 = fast1->next; result->next = intersectTwoLists(slow1, fast2); return result; } } // 返回交集链表,注意这里也可能有一个链表未遍历完 return slow1 ? intersectTwoLists(NULL, slow1) : intersectTwoLists(slow2, NULL); } // 打印链表 void printList(ListNode* head) { while (head) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { // 测试示例,自行替换实际链表数据 ListNode *list1 = createNode(4); list1->next = createNode(5); list1->next->next = createNode(6); // [4, 5, 6] ListNode *list2 = createNode(1); list2->next = createNode(3); list2->next->next = createNode(5); // [1, 3, 5] ListNode *intersection = intersectTwoLists(list1, list2); printList(intersection); return 0; } ``` 这个程序会找出两个有序链表中的公共元素,并将它们连接成一个新的链表。运行`main()`函数后,它会打印出交集链表[5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值