problem report: middle of linked list

Middle of Linked List

propose: get the middle element of a linked list

method: 1. use two pointers

conplexity: o(n)

example:

Given 1->2->3, return the node with value 2.

Given 1->2, return the node with value 1.

mycode:

        ListNode firstPointer = new ListNode(0);
        ListNode secondPointer = new ListNode(0);
        if (head == null) {
            return null;
        }
        firstPointer = head;
        secondPointer = head.next;

        if (secondPointer == null || secondPointer.next == null) {
            return firstPointer;
        }
 if (head == null || head.next == null) {
            return head;
        }

        ListNode slow = head, fast = head.next;

reflections: 1. the third element euqal to null, do not need to consider as an special case! The two return statement of if actually do the same things. Use head to represend the return value. Use two different pointers as return value looks massy. (Reconsider the special case, tell whether there is an easy way to represent,especially for several if statement)

      2. it‘s more reasonalbe to name the two pointer slow and fast

      3. differece of stack and heap variable

iterative body part:

mycode:

while( secondPointer.next != null && secondPointer.next.next != null) {
            secondPointer = secondPointer.next.next;
            firstPointer = firstPointer.next;
        }

        if (secondPointer.next != null) {
            firstPointer = firstPointer.next;
        }
        return firstPointer;

answers:

1  while (fast != null && fast.next != null) {
2             slow = slow.next;
3             fast = fast.next.next;
4         }
5
6         return slow;

refelection: consider null element in the end of linked list as an normal node. Test fast and fast.next in the while loop can simplify the code.

时间: 2024-10-03 06:22:57

problem report: middle of linked list的相关文章

WARN::Committed before 500 Unable to show problem report:|java.lang.IllegalStateException

2015-01-06 18:23:17.160:WARN::Committed before 500 Unable to show problem report:|java.lang.IllegalStateException: STREAM||Class: org.mortbay.jetty.Response|File: Response.java|Method: getWriter|Line: 616 - org/mortbay/jetty/Response.java:616:-1 2015

Struts2 Problem Report: No result defined for action ... and result exception

每    当Struts2爆出这样的异常,会很郁闷,原因太多了,只能一一的检查.有的说input页面没指定,有的说namespace有问题,有的说你对应的result有问题,有的说validate不通过,等等.但是在这里,我却不是,我无论如何都找不到原因,差不多花了两个钟,我无意中使用Struts2异常处理页面,把异常信息打印输出来(Integer不能转化为String ),我才知道原因的所在,原来是在Service层代码里执行hql语句时,传入的参数应该为String类型,我却把它弄为Inte

解决 HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException:

HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing: ==> msg [in template "org/apache/struts2/dispatcher/error.ftl" at line 68, column 31] Tip: If the fail

problem report: sort list

quick sort version 1: quick sort 定义: https://en.wikipedia.org/wiki/Quicksort quick sort 核心部分为partition(http://www.cnblogs.com/jiangchen/p/5398166.html), 取list中间点(http://www.cnblogs.com/jiangchen/p/5529704.html) 时间复杂度为o(nlgn) 1 public class Solution {

别的项目导入myeclipese出现struts problem report JDBC Exception:can not open connection解决办法

解决办法:数据库里缺少数据库,在对应的数据库里新建所需数据库即可

解决ubuntu invalid problem report

今天开机时出现如下对话框. 解决办法 sudo rm /var/lib/apt/lists/* -vf sudo apt-get update 参考: http://ubuntuforums.org/archive/index.php/t-1981033.html

Problem Linked List Cycle II

Problem Description: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space?  Solution: 1 public ListNode detectCycle(ListNode head) { 2 if (head == null) r

Problem Linked List Cycle

Problem Description: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space?  Solution: 1 public boolean hasCycle(ListNode head) { 2 if (head == null) return false; 3 if (head.next == null || head

problem-whether two headless linked lists cross

whether two headless linked lists cross 个人信息:就读于燕大本科软件工程专业 目前大三; 本人博客:google搜索"cqs_2012"即可; 个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献; 博客内容:在无头节点的链表里删除元素; 博客时间:2014-4-15; 编程语言:C++ ; 编程坏境:Windows 7 专业版 x64; 编程工具:vs2008 32位编译器; 制图工具:office 2010 ppt; 硬件信