LeetCode【876.連結串列的中間結點】
阿新 • • 發佈:2019-01-06
題目描述
給定一個帶有頭結點
head
的非空單鏈表,返回連結串列的中間結點。
如果,有兩個中間結點,則返回第二個中間節點。
示例 1:
- 輸入:[1,2,3,4,5]
- 輸出: 此列表中的結點 3 (序列化形式:[3,4,5])
返回的結點值為 3 。 (測評系統對該結點序列化表述是 [3,4,5])。
注意,我們返回了一個 ListNode 型別的物件 ans,這樣:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2 :
- 輸入:[1,2,3,4,5,6]
- 輸出:
由於該列表有兩個中間結點,值分別為 3 和 4,我們返回第二個結點。
思路 1
通過迴圈統計連結串列中變數的個數即count
,再次進行遍歷時,直接返回第結點即可,當用迴圈再次開始遍歷時,本身已經是,所以再移動次時,此時的已經是第結點。
程式碼 1
class Solution {
public ListNode middleNode(ListNode head) {
int count = 0;
ListNode pNode = head, qNode = head;
while(pNode != null) {
count ++;
pNode = pNode.next;
}
count = count / 2 ;
while(count != 0) {
count -- ;
qNode = qNode.next;
}
return qNode;
}
}
複雜度分析:
- 時間複雜度:,其中N是列表中的結點數目。
- 空間複雜度:,qNode,pNode用去的空間。
思路2
按順序將每個結點放到陣列A中,然後中間結點就是A[A.length/2],因此我們可以通過索引檢索每個結點。由於索引是從0開始的,所以中間節點的位置即是.
程式碼2
class Solution {
public ListNoe[] A = new ListNode[100];
int t = 0;
while(head.next != null) {
A[t++] = head;
head = head.next;
}
return A[t/2];
}
複雜度分析
- 時間複雜度:,其中N是列表中的結點數目。
- 空間複雜度:,A用去的空間。
思路3
當用慢指標slow遍歷列表時,讓另一個指標fast的速度是slow的二倍,則當快指標到結尾時,slow指標位於中間。初始位置都為head時,當fast指向最終的null時,slow也就達到了要求。
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while((fast != null) && (fast.next != null)) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
複雜度分析
- 時間複雜度:,其中N是列表中的結點數目。
- 空間複雜度:,fast,slow用去的空間。
完整程式碼
package cn.zcs.leetcode;
import java.io.*;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode middle (ListNode head) {
if(head == null)
return head;
ListNode fast = head, slow = head;
while(fast.next != null && fast != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
public class middleNode {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if(input.length() == 0) {
return new int[0];
}
String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index ++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}
public static ListNode stringToListNode(String input) {
int[] nodeValues = stringToIntegerArray(input);
ListNode dummyRoot = new ListNode(0);
ListNode ptr = dummyRoot;
for(int item : nodeValues) {
ptr.next = new ListNode(item);
ptr= ptr.next;
}
return dummyRoot.next;
}
public static String listNodeToString(ListNode node) {
if(node == null) {
return "[]";
}
String result = "";
while(node != null) {
result += Integer.toString(node.val) + ", ";
node = node.next;
}
return "[" + result.substring(0, result.length() - 2) + "]";
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
String line;
while((line = in.readLine()) != null) {
ListNode head = stringToListNode(line);
ListNode ret = new Solution().middle(head);
String out = listNodeToString(ret);
System.out.println(out);
}
}
}