[Reversed-LinkedList] https://leetcode.com/problems/reverse-linked-list/description/
1.Reversed Linked-list
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
while (head != null) {
ListNode temp = head.next;
head.next = prev;
prev = head;
head = temp;
}
return prev;
}
}
2.[Reversed Linked List II]https://leetcode.com/problems/reverse-linked-list-ii/description/
solution:重點(diǎn)是找到需要翻轉(zhuǎn)的區(qū)間,具體看注釋
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(0);// create a dummy node to mark the head of this list
dummy.next = head;
ListNode pre = dummy;// make a pointer pre as a marker for the node before reversing
for (int i=0; i<m-1; i++) {
pre = pre.next;
}
ListNode start = pre.next;// a pointer to the beginning of a sub-list that will be reversed
ListNode tail = start.next;// a pointer to a node that will be reversed
// 1 - 2 -3 - 4 - 5 ; m=2; n =4 ---> pre = 1, start = 2, tail = 3
// dummy-> 1 -> 2 -> 3 -> 4 -> 5
for (int i=0; i<n-m; i++) {
start.next = tail.next;
tail.next = pre.next;
pre.next = tail;
tail = start.next;
//this phase is the standard code for reverse Linked List,need to be one-to-one correspondence
}
return dummy.next;
}
}
3.判斷鏈表是否有環(huán)
[Linked List Cycle] https://leetcode.com/problems/linked-list-cycle/description/
solution:快慢指針
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode fast = head;
ListNode slow = head;
while (fast != null) {
if (fast.next == null) {
return false;
}
if (fase.next == slow) {
return true;
}
}
fast = fast.next.next;
slow = slow.next;
}
return false;
}
4.刪除鏈表倒數(shù)第N個(gè)節(jié)點(diǎn)
[Remove Nth Node form End of LinkedList]https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
solution:快慢指針,看注釋
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode start = new ListNode(0);
ListNode slow = start, fast = start;
slow.next = head;
//將fast先走侄刽,與slow的距離保證為n
for (int i=1; i<=n+1; i++) {
fast = fast.next;
}
//將fast走到頭泉孩,然后slow走到中間,兩者的距離正好還是n
while (fast != null) {
slow = slow.next;
fast = fast.next;
}
//刪除目標(biāo)節(jié)點(diǎn)
slow.next = slow.next.next;
return start.next;
}
}
5.刪除鏈表中的元素
[Remove Linked List Elements]https://leetcode.com/problems/remove-linked-list-elements/description/
solution:鏈表刪除的統(tǒng)一做法聪姿,要注意找到刪除節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
while (head.next != null) {
if (head.next.val == val) {
head.next = head.next.next;
} else {
head = head.next;//move head pointer to next and then loop
}
}
return dummy.next;
}
}
6.兩個(gè)鏈表第一個(gè)公共節(jié)點(diǎn)
solution:首先遍歷兩個(gè)鏈表得到它們的長(zhǎng)度碴萧,就知道哪個(gè)鏈表比較長(zhǎng),以及它的鏈表比斷的鏈表多幾個(gè)節(jié)點(diǎn)末购。在第二次遍歷的時(shí)候破喻,在較長(zhǎng)的鏈表上先走若干步,接著再同時(shí)在兩個(gè)鏈表上遍歷盟榴,找到第一個(gè)相同的節(jié)點(diǎn)就是它們的第一個(gè)公共節(jié)點(diǎn)曹质。
public ListNode findFirstCommonNode (ListNode pHead1, ListNode pHead2) {
if (pHead1 == null || pHead2 == null) {
return null;
}
//定義兩個(gè)指針
ListNode node1 = pHead1, node2 = pHead2;
int length1 = 0, length2 = 0;
//遍歷兩個(gè)鏈表
while (node1 != null) {
length1 += 1;
node1 = node1.next;
}
while (node2 != null) {
length2 += 1;
node2 = node2.next;
}
//對(duì)較長(zhǎng)鏈表的頭結(jié)點(diǎn)處理,先走差值k步
if (length1 > length2) {
int k = length1 - length2;
while (k != 0) {
pHead1 = pHead1.next;
k--;
}
} else {
int k = length2 - length1;
while (k != 0) {
pHead2 = pHead2.next;
k--;
}
}
//遍歷第一個(gè)相同的節(jié)點(diǎn)就是第一個(gè)公共節(jié)點(diǎn)
while (pHead1 != pHead2) {
pHead1 = pHead1.next;
pHead2 = pHead2.next;
}
return pHead1;
}
7.從尾到頭打印鏈表
solution:兼職offer版本,看代碼
//棧的方式
class Solution {
public static void printListReverse(ListNode head) {
Stack<ListNode> stack = new Stack<ListNode>();
if (head == null) {
return;
}
while (head != null) {
stack.push(head);
head = head.next;
}
while (!stack.empty()) {
stack.pop();
}
}
}
//遞歸的方式
class Solution {
public staic void pinrtListReverse(ListNode head) {
if (head == null) {
return;
}
while (head != null) {
if (head.next != null) {
ListNode next = head.next;
printListReverse(next);
} else {
return;
}
}
}
}