一、计算链表中节点个数
/**
* @param head 不计算头结点
* @return count 链表中的结点个数
*/
public static int getLength(Node head) {
if(head.next==null) {
return 0;
}
Node temp =head.next;
int count=0;
while(temp!=null){
count++;
temp=temp.next;
}
return count;
}
二、查找倒数第k个节点
/**
* @param k
* @param head
* @return 返回倒数第k个节点
*/
public static Node findIndexNode(int k,Node head){
if(head.next == null){
System.out.println("链表为空");
return:
}
int size = getLength(head);
int num = size-k; //要遍历的次数
Node temp = head.next;
for(int i =0;i<num;i++){
temp=temp.next;
}
return temp;
}
三、单链表的反转
public static void reverse(Node head) {
if(head.next ==null||head.next.next==null){
return;
}
Node cur=head.next;
Node next=null;
Node newList=new Node(0);
while(cur!=null){
next=cur.next; //next保存下一节点
cur.next =newList.next; //将cur的下一节点加入新链表的最前端
newList.next=cur; //当前节点链接到新链表
cur=next; //cur指向下一节点
}
head.next=newList.next;
}
四、从尾到头打印单链表
public static void reversePrint(Node head) {
Stack<Node> stack = new Stack();
Node temp = head.next;
//将所有节点压入栈
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
//当栈不为空时,弹栈并显示
while (!stack.isEmpty()){
System.out.println(stack.pop());
}
}
五、删除排序链表中的重复元素(不带头结点)
* 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; }
* }
public ListNode deleteDuplicates(ListNode head) {
if(head == null){
return null;
}
ListNode node = head;
while(node!=null&&node.next!=null){
if(node.val==node.next.val){
node.next= node.next.next;
}
else{
node=node.next;
}
}
return head;
}
六、判断链表是否有环(不带头结点)
快指针比慢指针每次走两步
* 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; }
* }
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;
}
七、返回环形链表中的第一个结点(不带头结点)
* 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; }
* }
public ListNode detectCycle(ListNode head) {
if( head == null || head.next ==null){
return null;
}
ListNode slow =head;
ListNode fast= head;
while(fast!=null){
slow = slow.next;
if(fast.next != null){
fast=fast.next.next;
}else{
return null;
}
if(fast == slow){
ListNode temp = head;
while(temp!=slow){
temp=temp.next;
slow=slow.next;
}
return temp;
}
}
return null;
八、两两交换链表中的结点(不带头结点)
* 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; }
* }
public ListNode swapPairs(ListNode head) {
ListNode pre =new ListNode(0);
pre.next =head;
ListNode temp = pre;
while(temp.next!=null&&temp.next.next!=null){
ListNode node1 = temp.next;
ListNode node2=temp.next.next;
temp.next= node2; //交换前后结点
node1.next =node2.next;
node2.next =node1;
temp=node1;
}
return pre.next;
}