380
来源:互联网 发布:网络摄像头怎么安装 编辑:程序博客网 时间:2024/05/14 20:06
5.20
先遍历一遍链表,存入ArrayList中。
然后从后向前找到相同的地方,在选取较短的链表从前向后遍历找到节点。
还是要注意 要使用equals 不要使用==。
郁闷,现在一个题总要提交很多很多遍才可以AC。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { /** * @param headA: the first list * @param headB: the second list * @return: a ListNode */ public ListNode getIntersectionNode(ListNode headA, ListNode headB) { // Write your code here if(headA == null || headB == null){ return null; } ArrayList<Integer> listA = new ArrayList<Integer>(); ArrayList<Integer> listB = new ArrayList<Integer>(); ListNode tmpA = headA; ListNode tmpB = headB; while(tmpA != null){ listA.add(tmpA.val); tmpA = tmpA.next; } while(tmpB != null){ listB.add(tmpB.val); tmpB = tmpB.next; } int la = listA.size() - 1; int lb = listB.size() - 1; //System.out.println("前la:" + la + ";lb:" + lb); while(la >= 0 && lb >= 0){ //System.out.println("listA.get(la):" + listA.get(la) + ";listB.get(lb):" + listB.get(lb)); if(listA.get(la).equals(listB.get(lb))){ la --; lb --; } else{ break; } } //System.out.println("后la:" + la + ";lb:" + lb); ListNode res = null; if(la == -1){ return headA; } if(lb == -1){ return headB; } if(la <= lb){ res = headA; while(la >= 0 && res.next != null){ res = res.next; la --; } } else{ res = headB; while(lb >= 0 && res.next != null){ res = res.next; lb--; } } return res; } }
阅读全文
0 0
- 380
- ocp-380
- Leetcode 380
- 380_ViewStub填充
- 租到了房子,380pm
- HP 380 cciss raid查看
- Codeforces Round #380 B - Spotlights
- codeforces Round #380 Div.2
- 380 型医用 X 光胶片洗片机
- 380元的USB数据采集板
- 免费下载380套大型商业源码
- 【小阅读^大脑袋】0628 NO.380
- 380伏电压一安是多少瓦?
- 源码推荐-380套大型商业源码
- 戴尔台式机380+ubuntu10.04声卡解决方法
- Code Forces 380 A. Sereja and Prefixes
- CF 380A Sereja and Prefixes(二分)
- Codeforces 380 A. Sereja and Prefixes
- 第22章 傅里叶变换性质
- Android设置EditText为单行 (软键盘显示下一项)
- SpringBoot--transaction
- mysql explain
- git使用总结
- 380
- 设置sublime
- git不同仓库不同账户解决方法
- 利用filter过滤器实现整个服务器对中文的支持
- 最近使用redis的总结
- liunx 安装mysql5.7.17
- 自定义滑动交互的圆形ProgressBar
- STM32F4应用笔记(四)UCGUI裸机移植——支持触摸屏
- git一个本地仓库提交到2个账户仓库