题目83.删除排序链表中的重复元素
-
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode current=head; while(current!=null&¤t.next!=null){ if(current.val==current.next.val){ current.next=current.next.next; }else current=current.next; } return head; } }
题目141.环形链表
-
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> seen =new HashSet<>(); while(head!=null){ if(!seen.add(head)) return true; else head=head.next; } return false; } }
-
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { //快慢指针,龟兔赛跑算法 if(head==null||head.next==null) return false; ListNode fast=head.next; ListNode slow=head; while(fast!=slow){ if(fast==null||fast.next==null) return false; else { fast=fast.next.next; slow=slow.next; } } return true; } }
题目160.相交链表
-
//Definition for singly-linked list. /* public class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } }*/ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) return null; ListNode pA = headA, pB = headB; while (pA != pB) { pA = pA == null ? headB : pA.next;//注意一定是headB,之前写成pb,一直提交不成功 pB = pB == null ? headA : pB.next; } return pA; } }
题目203.移除链表元素
- ```java
/**官方解法,设置哨兵节点,避免删除头部节点引起的麻烦
* Definition for singly-linked list.
*public class ListNode {
int val;
ListNode next;
ListNode() {}
* ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode sentinel=new ListNode(0);
sentinel.next=head;
ListNode prev=sentinel,curr=head;
while(curr!=null){
if(curr.val==val) prev.next=curr.next;
else prev=curr;
curr=curr.next;
}
return sentinel.next;
}
}
-
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeElements(ListNode head, int val) { //我的方法,判断本节点或下节点是否等于val。 ListNode current=head; while(current!=null) { if(head.val==val) {//头节点等于val if(head.next!=null){//判断是否只有一个节点,是则返回null,否则去掉头节点。 head=head.next; current=head; }else return null; }else if(current.next!=null&¤t.next.val==val){//判断下个节点是否存在是否等于val。 current.next=current.next.next; }else if(current.next!=null &¤t.next.val!=val){ current=current.next; }else //其他情况,下个节点为空的话,直接返回。 return head; } return head; } }
题目206.反转链表
```java
/** 迭代
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
//迭代
ListNode prev=null;
ListNode curr=head;
while(curr!=null){
ListNode next=curr.next;
curr.next=prev;
prev=curr;
curr=next;
}
return prev;
}
}
``
`java
-
//递归 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode reverseList(ListNode head) { /*//迭代 ListNode prev=null; ListNode curr=head; while(curr!=null){ ListNode next=curr.next; curr.next=prev; prev=curr; curr=next; } return prev;*/ //递归 //递归结束条件 //12345 2345 345 45 5当头节点为空或其下个节点为空(防止闭环),递归结束。 if(head==null||head.next==null) return head; ListNode newhead=reverseList(head.next); head.next.next=head;//上次输入的节点的下个节点应该是这次的头节点。 head.next=null;//这次节点之后的为空,543接上2,这次输入的2345中2后面的不要了。 return newhead; } }
题目234.回文链表
-
``
```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
//方法一:将链表的值添加到列表中
List<Integer> list=new ArrayList<>();
ListNode curr=head;
while(curr!=null){
list.add(curr.val);
curr=curr.next;
}
int i=0,j=list.size()-1;
while(i<j){
if(!list.get(i).equals(list.get(j)))return false;//这里为什么用equals()方法
i++;
j--;
}
return true;
}
}
- ```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
//方法二:递归,看不明白
private ListNode frontPointer;
private boolean recursivelyCheck(ListNode currentNode){
if(currentNode!=null){
if(!recursivelyCheck(currentNode.next)){
return false;
}
if(currentNode.val!=frontPointer.val){
return false;
}
frontPointer=frontPointer.next;
}
return true;
}
public boolean isPalindrome(ListNode head) {
frontPointer=head;
return recursivelyCheck(head);
}
}
-
```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
//方法二:递归,看不明白
private ListNode frontPointer;
private boolean recursivelyCheck(ListNode currentNode){
//递归结束条件
if(currentNode==null) return true;//当递归到链表结尾时停止。返回true。
if(!recursivelyCheck(currentNode.next)) return false;//上一层递归的返回值,若上一层返回false,判定进入,继续返回false,层层推进,最终返回false。
if(frontPointer.val==currentNode.val){//如果前面的值和后面值相等,继续递归,返回true,并把frontPointer指针加1,否则返回false,结束递归。
frontPointer=frontPointer.next;
return true;
}else return false;
}
public boolean isPalindrome(ListNode head){
frontPointer=head;
return recursivelyCheck(head);
}
}
- 自己写的递归代码,思想参考的解答。