思路相對簡單,考察代碼實現(xiàn)能力及其整潔高效。
206.Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
思路:把當前節(jié)點的指針指向前面節(jié)點(助記:調(diào)轉(zhuǎn)槍口)
需要兩個指針,一個記錄當前節(jié)點,一個記錄前面節(jié)點
while循環(huán)到鏈表結(jié)束
測試用例:輸入的鏈表頭指針是nullptr
輸入的鏈表只有一個節(jié)點
輸入的鏈表有多個節(jié)點
code:
class Solution {
public ListNode reverseList(ListNode head) {//返回類型為ListNode
ListNode prev = null;//定義前驅(qū)節(jié)點為空
ListNode curr = head;//定義當前節(jié)點為頭節(jié)點
while (curr != null) {//當前節(jié)點不為空時,一直循環(huán)至鏈表結(jié)束
ListNode nextTemp = curr.next;//標記出下一個操作的節(jié)點,防止斷鏈
curr.next = prev;//當前節(jié)點指針調(diào)轉(zhuǎn)槍口指向前驅(qū)節(jié)點
prev = curr;//當前節(jié)點完成后改為前驅(qū)節(jié)點
curr = nextTemp;//現(xiàn)有下一個節(jié)點改為當前需要操作的節(jié)點
}
return prev; //返回前面節(jié)點
}
}
-
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
思路:鏈表交換相鄰元素
三個指針:相鄰兩個元素+前一個元素
代碼不超過十行
測試用例:輸入的鏈表頭指針是nullptr
輸入的鏈表只有一個節(jié)點
輸入的鏈表有多個節(jié)點
code:
public class Solution {
public ListNode swapPairs(ListNode head) {
if ((head == null)||(head.next == null))
return head;
ListNode n = head.next;
head.next = swapPairs(head.next.next);
n.next = head;
return n;
}
}
image.png
- Linked List Cycle
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
思路:法一:從前往后推,直到遇到空指針(硬做,性能差)
法二:每個節(jié)點用set存儲,到新節(jié)點對set判重(O(n*1))
法三:快慢指針法(快指針走兩步,慢指針走一步,若相遇則有環(huán))O(n)(龜兔賽跑追擊問題)
code:
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;//初始化慢指針
ListNode fast = head.next;//初始化快指針
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
- Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Note: Do not modify the linked list.
思路:第一步:確定是否包含環(huán),同上題
第二步:快慢指針相遇的節(jié)點一定在環(huán)中,從相遇節(jié)點出發(fā),一邊繼續(xù)向前移動一邊計數(shù),再次回到這個節(jié)點時,即得環(huán)中節(jié)點數(shù)。
第三步:找環(huán)的入口:定義兩個指針指向頭節(jié)點,若環(huán)有n個節(jié)點,則指針一先向前移動n個節(jié)點,然后倆指針以相同速度向前移動,當指針二指向環(huán)的入口節(jié)點時,指針一繞著環(huán)走了一圈又回到了入口節(jié)點。
code:
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if (fast == slow){
ListNode slow2 = head;
while (slow2 != slow){
slow = slow.next;
slow2 = slow2.next;
}
return slow;
}
}
return null;
}
}
- Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
Example:
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
思路:法一:棧
法二:尾插法
法三:遞歸
code:
class Solution{
public ListNode reverseKGroup(ListNode head, int k) {
ListNode curr = head;
int count = 0;
while (curr != null && count != k) { // find the k+1 node
curr = curr.next;
count++;
}
if (count == k) { // if k+1 node is found
curr = reverseKGroup(curr, k); // reverse list with k+1 node as head
// head - head-pointer to direct part,
// curr - head-pointer to reversed part;
while (count-- > 0) { // reverse current k-group:
ListNode tmp = head.next; // tmp - next head in direct part
head.next = curr; // preappending "direct" head to the reversed list
curr = head; // move head of reversed part to a new node
head = tmp; // move "direct" head to the next node in direct part
}
head = curr;
}
return head;
}
}