import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class ListNode { public int val; public ListNode next; public ListNode(int val) { this.val = val; this.next = null; } } public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int m = Integer.parseInt(br.readLine()); String[] strList1 = br.readLine().trim().split(" "); int n = Integer.parseInt(br.readLine()); String[] strList2 = br.readLine().trim().split(" "); ListNode head1 = new ListNode(Integer.parseInt(strList1[0])); ListNode cur1 = head1; for(int i = 1; i < m; i++){ cur1.next = new ListNode(Integer.parseInt(strList1[i])); cur1 = cur1.next; } ListNode head2 = new ListNode(Integer.parseInt(strList2[0])); ListNode cur2 = head2; for(int i = 1; i < n; i++){ cur2.next = new ListNode(Integer.parseInt(strList2[i])); cur2 = cur2.next; } while(head1 != null && head2 != null){ if(head1.val < head2.val){ head1 = head1.next; }else if(head1.val > head2.val){ head2 = head2.next; }else{ System.out.print(head1.val + " "); head1 = head1.next; head2 = head2.next; } } } }
#include<iostream> using namespace std; struct ListNode { ListNode* next; int val; ListNode(int val) : next(nullptr),val(val){} }; int main() { long long n,m; cin >> n; ListNode L1(-1); ListNode* tmp = &L1; for(int i = 0;i<n;i++) { int x; cin >> x; ListNode* newnode = new ListNode(x); tmp->next = newnode; tmp = newnode; } ListNode L2(-1); tmp = &L2; cin >> m; for(int i = 0;i<m;i++) { int x; cin >> x; ListNode* newnode = new ListNode(x); tmp->next = newnode; tmp = newnode; } ListNode* head1 = L1.next; ListNode* head2 = L2.next; while(head1!=nullptr && head2!=nullptr) { if(head1->val <head2->val) head1 = head1->next; else if(head1->val > head2->val) head2 = head2->next; else { cout << head1->val << " "; head1 = head1->next; head2 = head2->next; } } cout << endl; }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static class Node { public int value; public Node next; public Node(int value) { this.value = value; } } public static void printCommonPart(Node head1, Node head2) { if (head1 == null || head2 == null) { return; } while (head1 != null && head2 != null) { if (head1.value > head2.value) { head2 = head2.next; } else if (head1.value < head2.value) { head1 = head1.next; } else { System.out.print(head1.value + " "); head1 = head1.next; head2 = head2.next; } } System.out.println(); } private static Node listGenerator(int length, String[] numbers) { Node head = new Node(Integer.parseInt(numbers[0])); Node cur = head; for (int i = 1; i < length; i++) { cur.next = new Node(Integer.parseInt(numbers[i])); cur = cur.next; } cur.next = null; return head; } public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(bufferedReader.readLine()); String[] numbers1 = bufferedReader.readLine().split(" "); int m = Integer.parseInt(bufferedReader.readLine()); String[] numbers2 = bufferedReader.readLine().split(" "); Node head1 = listGenerator(n, numbers1); Node head2 = listGenerator(m, numbers2); printCommonPart(head1, head2); } }
# include <bits/stdc++.h> using namespace std; struct list_node{ int val; struct list_node * next; }; //链表的节点 list_node * input_list(void) //读入链表 { int n, val; list_node * phead = new list_node(); list_node * cur_pnode = phead; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &val); if (i == 1) { cur_pnode->val = val; cur_pnode->next = NULL; } else { list_node * new_pnode = new list_node(); new_pnode->val = val; new_pnode->next = NULL; cur_pnode->next = new_pnode; cur_pnode = new_pnode; } } return phead; } void sol(list_node * a_head, list_node * b_head) { while(a_head && b_head){ if(a_head->val == b_head->val){ cout<<a_head->val<<" "; a_head = a_head->next; b_head = b_head->next; }else if(a_head->val > b_head->val) b_head = b_head->next; else a_head = a_head->next; } } int main () { list_node * a_head = input_list(); // A 链表的头节点 list_node * b_head = input_list(); // B 链表的头节点 sol(a_head, b_head); return 0; }
# include <bits/stdc++.h> using namespace std; struct list_node{ int val; struct list_node * next; }; //链表的节点 list_node * input_list(void) //读入链表 { int n, val; list_node * phead = new list_node(); list_node * cur_pnode = phead; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &val); if (i == 1) { cur_pnode->val = val; cur_pnode->next = NULL; } else { list_node * new_pnode = new list_node(); new_pnode->val = val; new_pnode->next = NULL; cur_pnode->next = new_pnode; cur_pnode = new_pnode; } } return phead; } void sol(list_node * a_head, list_node * b_head) { //////在下面完成代码 while (a_head && b_head) { if (a_head->val == b_head->val) { cout << a_head->val << " "; a_head = a_head->next; b_head = b_head->next; } else if (a_head->val < b_head->val) a_head = a_head->next; else b_head = b_head->next; } } int main () { list_node * a_head = input_list(); // A 链表的头节点 list_node * b_head = input_list(); // B 链表的头节点 sol(a_head, b_head); return 0; }
# include <bits/stdc++.h> using namespace std; struct list_node{ int val; struct list_node * next; }; //链表的节点 list_node * input_list(void) //读入链表 { int n, val; list_node * phead = new list_node(); list_node * cur_pnode = phead; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &val); if (i == 1) { cur_pnode->val = val; cur_pnode->next = NULL; } else { list_node * new_pnode = new list_node(); new_pnode->val = val; new_pnode->next = NULL; cur_pnode->next = new_pnode; cur_pnode = new_pnode; } } return phead; } void sol(list_node * a_head, list_node * b_head) { //////在下面完成代码 while(a_head&&b_head) { int a=a_head->val,b=b_head->val; if(a==b) { cout<<a<<" "; a_head=a_head->next; b_head=b_head->next; } else if(a>b) b_head=b_head->next; else a_head=a_head->next; } } int main () { list_node * a_head = input_list(); // A 链表的头节点 list_node * b_head = input_list(); // B 链表的头节点 sol(a_head, b_head); return 0; }
void sol(list_node * a_head, list_node * b_head)
{
//////在下面完成代码
list_node * p1 = a_head;
list_node * p2 = b_head;
while(p1 && p2){
if(p1->val == p2->val){
cout << p1->val << " ";
p1 = p1->next;
p2 = p2->next;
}
else if(p1->val < p2->val){
p1 = p1->next;
if(p1 == nullptr)
return;
}
else{
p2 = p2->next;
if(p2 == nullptr)
return;
}
}
}
n = int(input()) ls1 = list(map(int,input().split())) m = int(input()) ls2 = list(map(int,input().split())) i,j = 0,0 ls = [] while i < n and j < m: if ls1[i] == ls2[j]: ls.append(ls1[i]) i += 1 j += 1 else: if ls1[i] < ls2[j]: i += 1 else: j += 1 ls.sort() print(' '.join(map(str,ls)))
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); String[] rawInput = br.readLine().trim().split(" "); Node firstList = getInputLinkedList(rawInput, n); int m = Integer.parseInt(br.readLine().trim()); rawInput = br.readLine().trim().split(" "); Node secondList = getInputLinkedList(rawInput, m); String res = getListCommon(firstList, secondList); System.out.print(res); br.close(); } private static Node getInputLinkedList(String[] rawInput, int length) { Node head = null, curNode = null; for (int i = 0; i < length; i++) { Node tmp = new Node(Integer.parseInt(rawInput[i]));; if (null == head) { head = tmp; } else { curNode.next = tmp; } curNode = tmp; } return head; } private static String getListCommon(Node first, Node second) { StringBuilder sb = new StringBuilder(); while (null != first && null != second) { if (first.value == second.value) { sb . append(first.value) . append(" "); first = first.next; second = second.next; } else if (first.value > second.value) { second = second.next; } else { first = first.next; } } return sb.toString().trim(); } } class Node { public int value; public Node next; public Node(int value) { this.value = value; next = null; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] nums1 = new int[n]; for (int i = 0; i < n; i++) { nums1[i] = scan.nextInt(); } int m = scan.nextInt(); int[] nums2 = new int[m]; for (int i = 0; i < m; i++) { nums2[i] = scan.nextInt(); } int index1 = 0, index2 = 0; while (index1 < n && index2 < m) { if (nums1[index1] == nums2[index2]) { System.out.print(nums1[index1] + " "); index1++; index2++; } else if (nums1[index1] < nums2[index2]) { index1++; } else { index2++; } } } }
class listNode: def __init__(self,x): self.value=x self.next=None def list2linkList(arr): if len(arr)==0: return None l=1;head=listNode(arr[0]) pre=head while l<=len(arr): pre.next=listNode(arr[l-1]) pre=pre.next l+=1 return head.next def printSame(head1,head2): if head1==None&nbs***bsp;head2==None: return cur1=head1;cur2=head2 while cur1 and cur2: if int(cur1.value)<int(cur2.value): cur1=cur1.next elif int(cur1.value)>int(cur2.value): cur2=cur2.next else: print(cur1.value), cur1=cur1.next cur2=cur2.next return n1=int(input()) arr1=raw_input().split(' ') n2=int(input()) arr2=raw_input().split(' ') printSame(list2linkList(arr1),list2linkList(arr2))python 超出内存,75%