(1)Remove Nth Node From End of List
解題思路:
hashmap。題目要求只使用一次遍歷。可以使用指針來完成單程解決方案。快速移動一個指針向前移動n + 1個位置,以保持兩個指針之間的間隙,然后以相同的速度移動兩個指針。
最后,當快速指針到達結束時,慢指針將在n + 1個位置后面 - 只是正確的點,以便能夠跳過下一個節點。
代碼如下:
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 public class Solution { 10 public ListNode removeNthFromEnd(ListNode head, int n) { 11 ListNode start = new ListNode(0); 12 ListNode fast = start, slow = start; 13 slow.next = head; 14 15 for (int i = 1; i <= n+1; i++) { 16 fast = fast.next; 17 } 18 19 while (fast != null) { 20 slow = slow.next; 21 fast = fast.next; 22 } 23 24 slow.next = slow.next.next; 25 return start.next; 26 } 27 }
(2)Linked List Cycle
linkedlist接口、
解題思路:
使用快慢引用的思路。兩個引用都指向鏈表頭,從鏈表頭開始遍歷,慢引用每次前進一步,而快引用每次前進兩步,如果鏈表是有環路的,那么快引用終將追上慢引用;如果沒有環路,那么遍歷就會有結束的時候
代碼如下:
1 /** 2 * Definition for singly-linked list. 3 * class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 public Boolean hasCycle(ListNode head) { 14 if (head == null) { 15 return false; 16 } 17 18 ListNode fast, slow; 19 fast = head; 20 slow = head; 21 while (fast.next != null && fast.next.next != null) { 22 slow = slow.next; 23 fast = fast.next.next; 24 if(fast == slow) { 25 return true; 26 } 27 } 28 return false; 29 } 30 }
corrupted double-linked list。(3)Remove Linked List Elements
解題思路:簡單明了,遍歷整個鏈表,遇到相應值得節點刪掉即可。
代碼如下:
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 public class Solution { 10 public ListNode removeElements(ListNode head, int val) { 11 ListNode dummy = new ListNode(0); 12 dummy.next = head; 13 head = dummy; 14 while (head.next != null) { 15 if (head.next.val == val) { 16 head.next = head.next.next; 17 } else { 18 head = head.next; 19 } 20 } 21 return dummy.next; 22 } 23 }
linkedlist添加元素、(4)Intersection of Two Linked Lists
解題思路:
1,獲取兩個列表的長度。2,將它們對齊到相同的起點。3,將它們一起移動,直到找到交點,或者結束null
java ArrayList,代碼如下:
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 public ListNode getIntersectionNode(ListNode headA, ListNode headB) { 14 int lenA = length(headA); 15 int lenB = length(headB); 16 // move headA and headB to the same start point 17 while (lenA > lenB) { 18 headA = headA.next; 19 lenA--; 20 } 21 while (lenA < lenB) { 22 headB = headB.next; 23 lenB--; 24 } 25 // find the intersection until end 26 while (headA != headB) { 27 headA = headA.next; 28 headB = headB.next; 29 } 30 return headA; 31 32 } 33 private int length (ListNode node){ 34 int length = 0; 35 while (node != null) { 36 node = node.next; 37 length++; 38 } 39 return length; 40 } 41 }
?(5)Palindrome Linked List
解題思路:
找到鏈表的中間節點,把后半段的原地鏈表反轉(當然也可以反轉前半段),然后和前半段進行比較,符合題目O(1)空間復雜度的要求。
代碼如下:
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 public class Solution { 10 public boolean isPalindrome(ListNode head) { 11 if (head == null || head.next == null) {//0個節點或是1個節點 12 return true; 13 } 14 //找到中間節點 15 ListNode fast = head; 16 ListNode slow = head; 17 while (fast.next != null && fast.next.next != null) 18 { 19 fast = fast.next.next; 20 slow = slow.next; 21 } 22 //對鏈表后半段進行反轉 23 ListNode midNode = slow; 24 ListNode firNode = slow.next;//后半段鏈表的第一個節點 25 ListNode cur = firNode.next;//插入節點從第一個節點后面一個開始 26 firNode.next = null;//第一個節點最后會變最后一個節點 27 while (cur != null) 28 { 29 ListNode nextNode = cur.next;//保存下次遍歷的節點 30 cur.next = midNode.next; 31 midNode.next = cur; 32 cur = nextNode; 33 } 34 35 //反轉之后對前后半段進行比較 36 fast = midNode.next; 37 slow = head; 38 while (fast != null) 39 { 40 if (fast.val != slow.val) 41 return false; 42 slow = slow.next; 43 fast = fast.next; 44 } 45 return true; 46 } 47 }
?