Linked List Start!

(1)Delete Node in a Linked List

题意简单明了,用后一个节点来替换要删除的节点即可。代码如下:

 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 void deleteNode(ListNode node) {
11         node.val = node.next.val;
12         node.next = node.next.next;
13     }
14 }
View Code

 (2)Reverse Linked List

解题方法一:递归法

在反转当前节点之前先反转后续节点。这样从头结点开始,层层深入直到尾结点才开始反转指针域的指向。简单的说就是从尾结点开始,逆向反转各个结点的指针域指向。

 代码如下:

 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 reverseList(ListNode head) {  
11         if (head == null) {
12             return null;
13         }
14         if (head.next == null) {
15             return head;
16         }//递归解法一定要有判断条件:此时说明到达反转前的尾节点,可以开始执行反转操作
17         ListNode p = head.next;  
18         ListNode n = reverseList(p);  
19         p.next = head;  //当前节点的指针域指向前一节点
20         head.next = null;  //前一节点的指针域置null
21         return n;  
22     }  
23 }
View Code

解题方法二:迭代法

递归反转法是从后往前逆序反转指针域的指向,而遍历反转法是从前往后反转各个结点的指针域的指向。
 基本思路是:将当前节点cur的下一个节点 cur.Next缓存到temp后,然后更改当前节点指针指向上一结点pre。也就是说在反转当前结点指针指向前,先把当前结点的指针域用tmp临时保存,以便下一次使用。
代码如下:
 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 reverseList(ListNode head) {
11         ListNode pre = null;// 第一个节点肯定是(反转后的)尾节点,尾节点的next为NULL
12         while (head != null) {//当前节点为空,说明到了(反转前的)尾节点
13             ListNode temp = head.next;//当前节点的下一个指针临时储存
14             head.next = pre;//当前节点指针指向上一个节点
15             //指针向下移动
16             pre = head;
17             head = temp;
18         }
19         return pre;
20     }
21 }
View Code

 (3)Remove Duplicates from Sorted List

解题思路:遍历一次,遇到重复的节点删除(将前一节点的指针指向该节点的下一个节点)即可。

代码如下:

 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 deleteDuplicates(ListNode head) {
11         if (head == null) {
12             return null;
13         }
14         ListNode node = head;
15         while (node.next != null)  {
16             if (node.val == node.next.val) {
17                 node.next = node.next.next;
18             } else {
19                 node = node.next;
20             }
21         }
22         return head;
23     }
24 }
View Code

(4)Merge Two Sorted Lists

 

解题思路:简单明了,创建一个新节点dummy作为头结点,然后l1和l2进行比较,小的先加入,哪个链表有剩余最后加入即可。

代码如下:

 1 * Definition for singly-linked list.
 2  * public class ListNode {
 3  *     int val;
 4  *     ListNode next;
 5  *     ListNode(int x) { val = x; }
 6  * }
 7  */
 8 public class Solution {
 9     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
10         ListNode dummy = new ListNode(0);
11         ListNode lastNode = dummy;
12         
13         while (l1 != null && l2 != null) {
14             if (l1.val < l2.val) {
15                 lastNode.next = l1;
16                 l1 = l1.next;
17             } else {
18                 lastNode.next = l2;
19                 l2 = l2.next;
20             }
21             lastNode = lastNode.next;
22         }
23         
24         if (l1 != null) {
25             lastNode.next = l1;
26         } else {
27             lastNode.next = l2;
28         }
29         return dummy.next;
30     }
31 }
View Code

 (5)Swap Nodes in Pairs

解题思路:n1、n2为第一组结点,头结点的next指向n2,n1的next指向n2的next,n2的next指向n1。然后进行下一组...

代码如下:

 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 swapPairs(ListNode head) {
11         ListNode dummy = new ListNode(0);
12         dummy.next = head;//新节点的next指针指向原链表的头结点
13         
14         head = dummy;//现在的头结点就是dummy
15         while (head.next != null && head.next.next != null) {
16             ListNode n1 = head.next;
17             ListNode n2 = head.next.next;
18             // head->n1->n2->...
19             // => head->n2->n1->...
20             head.next = n2;
21             n1.next = n2.next;
22             n2.next = n1;
23             //移向下一组节点
24             head = n1;
25         }
26         return dummy.next;
27     }
28 }
View Code
原文地址:https://www.cnblogs.com/struggleli/p/6189123.html