LeetCode上面的题目例如以下:

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3

begin to intersect at node c1.

Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.
我的解决方法例如以下:
/**

 * Definition for singly-linked list.

 * public class ListNode {

 *     int val;

 *     ListNode next;

 *     ListNode(int x) {

 *         val = x;

 *         next = null;

 *     }

 * }

 */

public class Solution {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        

int lengthA = 0,lengthB = 0;

for(ListNode head= headA;head!=null;head = head.next)

{

lengthA++;

}

for(ListNode head = headB;head!=null;head = head.next)

{

lengthB++;

}

if(lengthA>=lengthB)

{

for(int i=0;i<lengthA-lengthB;i++)

{

headA = headA.next;

}

}

else

{

for(int i=0;i<lengthB-lengthA;i++)

{

headB = headB.next;

}

}

for(ListNode newA =headA,newB = headB;newA!=null&&newB!=null;newA = newA.next,newB = newB.next)

{

if(newA==newB)

{

return newA;

}

}

return null;

    }

}

最新文章

  1. 在ASP.NET MVC 中获取当前URL、controller、action
  2. 神奇的VIM~转IBM
  3. KETTLE实现数据的删除和更新
  4. ural 1341. Device
  5. js原生捕鱼达人(一)
  6. boost::circular_buffer
  7. 【阿里云产品公测】结构化数据服务OTS之JavaSDK初体验
  8. Oracle创建触发器实现主键自增
  9. PDFium-PDF开源之旅(1)-初探代码下载编译
  10. Android的PackageManager的使用
  11. sqlalchemy相关知识
  12. POJ - 3666 Making the Grade(dp+离散化)
  13. 中国天气网 JSON接口的城市编码解析及结果
  14. 【JMeter】生成报告-Dashboard Report
  15. OAF--基础
  16. centos7 mysql5.7安装
  17. 深度学习笔记(八)Focal Loss
  18. SQL 、NoSQL数据库教程
  19. Lecture 5
  20. django创建分页

热门文章

  1. 远程映射错误 “发生系统错误 1312 指定的登录会话不存在。可能已被终止 IIS 访问 远程共享目录”
  2. NKOI 1469 通向自由的钥匙
  3. css sticky footer 布局 手机端
  4. ++x和x++
  5. LeetCode OJ--Merge Intervals @
  6. HDU 6251 Inkopolis(2017 CCPC-Final,I题,环套树 + 结论)
  7. Codeforces Round #490 (Div. 3)
  8. Apollo 分布式配置中心
  9. protobuf3 语法解析
  10. eos智能合约执行流程