forked from qiyuangong/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f24b1ba
commit f6325f5
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode(int x) { val = x; } | ||
* } | ||
*/ | ||
class Solution { | ||
// public ListNode middleNode(ListNode head) { | ||
// List<ListNode> array = new ArrayList<ListNode>(); | ||
// while (head != null) { | ||
// array.add(head); | ||
// head = head.next; | ||
// } | ||
// return array.get(array.size() / 2); | ||
// } | ||
public ListNode middleNode(ListNode head) { | ||
ListNode fast, slow; | ||
fast = slow = head; | ||
while (fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
return slow; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.next = None | ||
|
||
class Solution(object): | ||
# def middleNode(self, head): | ||
# """ | ||
# :type head: ListNode | ||
# :rtype: ListNode | ||
# """ | ||
# res = [] | ||
# while head: | ||
# res.append(head) | ||
# head = head.next | ||
# return res[len(res) / 2] | ||
|
||
def middleNode(self, head): | ||
# Fast point is 2 times faster than slow point | ||
fast = slow = head | ||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
return slow |