Chapter six Linked List & Array(鏈表與數組)
1.reverse-nodes-in-k-group(k組翻轉鏈表)【hard】
給你一個鏈表以及一個k,將這個鏈表從頭指針開始每k個翻轉一下。鏈表元素個數不是k的倍數,最後剩余的不用翻轉。
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { /** * @param head a ListNode *View Code@param k an integer * @return a ListNode */ public ListNode reverseKGroup(ListNode head, int k) { // Write your code here if (head == null || k <= 1) { return head; } ListNode dummy = new ListNode(0); dummy.next = head; head = dummy;while (head.next != null) { head = reverseNextK(head, k); } return dummy.next; } private ListNode reverseNextK(ListNode head, int k) { ListNode node = head; for (int i = 0; i < k; i++) { if (node.next == null) { returnhead.next; } node = node.next; } ListNode n1 = head.next; ListNode prev = head; ListNode curt = head.next; for (int i = 0; i < k; i++) { ListNode temp = curt.next; curt.next = prev; prev = curt; curt = temp; } n1.next = curt; head.next = prev; return n1; } }
註意:n0->n1->n2->...->nk->nk+1若要翻轉n1->...->nk,則n0到nk節點都會變化。 手動創建dummy node(哨兵節點),dummy.next總是head(頭結點),最後返回dummy.next也即head節點。reverseNextK()函數分三步:1.檢查是否有足夠的k個節點可翻轉(如果沒有,返回head.next,因為此時的head節點實際已在上次操作中被翻轉)2.翻轉( n1 = head.next; prev = head; curt = head.next;for (int i = 0; i < k; i++) { temp = curt.next; curt.next = prev; prev = curt; curt = temp;})3.鏈接,以便繼續下次翻轉(n1.next = curt; head.next = prev; return n1;)
2.reverse-linked-list(翻轉鏈表)
翻轉一個鏈表。給出一個鏈表1->2->3->null,這個翻轉後的鏈表為3->2->1->null。
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The head of linked list. * @return: The new head of reversed linked list. */ public ListNode reverse(ListNode head) { // write your code here ListNode prev = null; ListNode curt = head; while (curt != null) { ListNode temp = curt.next; curt.next = prev; prev = curt; curt = temp; } return prev; } }View Code
3.partition-list(鏈表劃分)
給定一個單鏈表和數值x,劃分鏈表使得所有小於x的節點排在大於等於x的節點之前。你應該保留兩部分內鏈表節點原有的相對順序。
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param head: The first node of linked list. * @param x: an integer * @return: a ListNode */ public ListNode partition(ListNode head, int x) { // write your code here if (head == null) { return head; } ListNode leftDummy = new ListNode(0); ListNode rightDummy = new ListNode(0); ListNode left = leftDummy; ListNode right = rightDummy; while (head != null) { if (head.val < x) { left.next = head; left = head; } else { right.next = head; right = head; } head = head.next; } left.next = rightDummy.next; right.next = null; return leftDummy.next; } }View Code
註意:定義leftDummy和rightDummy兩個哨兵節點,分別用來保存<x和≥x節點,最後進行鏈接即可。在left(right).next=head之後,left(right)也要=head。
4.merge-two-sorted-lists(合並兩個排序鏈表)
將兩個排序鏈表合並為一個新的排序鏈表。
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ public class Solution { /** * @param ListNode l1 is the head of the linked list * @param ListNode l2 is the head of the linked list * @return: ListNode head of linked list */ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { // write your code here ListNode dummy = new ListNode(0); ListNode head = dummy; while (l1 != null && l2 != null) { if (l1.val < l2.val) { head.next = l1; l1 = l1.next; } else { head.next = l2; l2 = l2.next; } head = head.next; } if (l1 != null) { head.next = l1; } else { head.next = l2; } return dummy.next; } }View Code
註意:經典合並法。當兩個鏈表都不為空時,比較節點值的大小...當其中一個鏈表不為空時...
Chapter six Linked List & Array(鏈表與數組)