File tree 5 files changed +76
-0
lines changed
algorithms/MiddleOfTheLinkedList
5 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -219,6 +219,7 @@ All solutions will be accepted!
219
219
| 458| [ Poor Pigs] ( https://leetcode-cn.com/problems/poor-pigs/description/ ) | [ java/py/js] ( ./algorithms/PoorPigs ) | Easy|
220
220
| 558| [ Quad Tree Intersection] ( https://leetcode-cn.com/problems/quad-tree-intersection/description/ ) | [ java/py] ( ./algorithms/QuadTreeIntersection ) | Easy|
221
221
| 874| [ Walking Robot Simulation] ( https://leetcode-cn.com/problems/walking-robot-simulation/description/ ) | [ java/py/js] ( ./algorithms/WalkingRobotSimulation ) | Easy|
222
+ | 876| [ Middle Of The Linked List] ( https://leetcode-cn.com/problems/middle-of-the-linked-list/description/ ) | [ java/py/js] ( ./algorithms/MiddleOfTheLinkedList ) | Easy|
222
223
223
224
# Database
224
225
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Middle Of The Linked List
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+ public ListNode middleNode (ListNode head ) {
11
+ int length = 0 ;
12
+ ListNode p = head ;
13
+
14
+ while (p != null ) {
15
+ p = p .next ;
16
+ length ++;
17
+ }
18
+ p = head ;
19
+
20
+ for (int i = 0 ; i < length / 2 ; i ++) {
21
+ p = p .next ;
22
+ }
23
+ return p ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val) {
4
+ * this.val = val;
5
+ * this.next = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {ListNode }
11
+ */
12
+ var middleNode = function ( head ) {
13
+ let length = 0 ,
14
+ p = head
15
+
16
+ while ( p ) {
17
+ p = p . next
18
+ length ++
19
+ }
20
+ p = head
21
+
22
+ for ( let i = 0 ; i < parseInt ( length / 2 ) ; i ++ ) {
23
+ p = p . next
24
+ }
25
+
26
+ return p
27
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.next = None
6
+
7
+ class Solution (object ):
8
+ def middleNode (self , head ):
9
+ """
10
+ :type head: ListNode
11
+ :rtype: ListNode
12
+ """
13
+ length = 0
14
+ p = head
15
+ while p :
16
+ p = p .next
17
+ length += 1
18
+ p = head
19
+ for i in xrange (length / 2 ):
20
+ p = p .next
21
+ return p
You can’t perform that action at this time.
0 commit comments