單鏈表的13道面試題詳解 含力扣牛客鏈結

2021-10-19 12:42:26 字數 3667 閱讀 6670

點我跳轉題目鏈結

class

solution

else

cur = cur.next;}if

(head.val == val)

return head;

}}

點我跳轉題目鏈結

方法一:

//改變前後節點指向

public listnode reverselist

(listnode head)

cur.next = pre;

pre = cur;

cur = curnext;

}return head;

}

方法二:

//頭插法

public listnode reverselist1 (listnode head)

listnode cur = head.next;

head.next = null;

while

(cur != null)

return head;

}

點我跳轉題目鏈結

public listnode middlenode

(listnode head)

return slow;

}

點我跳轉題目鏈結

方法一:

//第一次

public listnode findkthtotail

(listnode head,

int k)

while

(fast != null)

if(count < k)

return null;

return slow;

}

方法二:

//優化

public listnode findkthtotail1

(listnode head,

int k)

else

}while

(fast != null)

return slow;

}

點我跳轉題目鏈結

public listnode mergetwolists

(listnode l1, listnode l2)

else

tmp = tmp.next;}if

(l2 == null)

else

head = head.next;

return head;

}

6、鍊錶分割

點我跳轉題目鏈結

public listnode partition

(listnode phead,

int x)

else

cur = cur.next;

} a.next = headb.next;

b.next = null;

heada = heada.next;

return heada;

}

點我跳轉題目鏈結

public listnode deleteduplication1

(listnode phead)

}else

cur = cur.next;

} newhead = newhead.next;

tmp.next = null;

return newhead;

}

點我跳轉題目鏈結

public

boolean

chkpalindrome

(listnode a)

//然後翻轉中間節點後面節點的指向

listnode cur = slow.next;

slow.next =null;

while

(cur != null)

//最後從後往前 以及 從前往後 比較每個節點的值

while

(slow != null)

slow = slow.next;

a = a.next;

}return

true

;}

點我跳轉題目鏈結

public listnode getintersectionnode

(listnode heada, listnode headb)

}else

}while

(cura != curb)

return cura;

}public

intsize

(listnode head)

return count;

}

點我跳轉題目鏈結

public

boolean

hascycle

(listnode head)

while

(slow != fast)

;return

true

;}

點我跳轉題目鏈結

public listnode detectcycle

(listnode head)

while

(slow != fast)

; slow = head;

while

(slow != fast)

return fast;

}

點我跳轉題目鏈結

public

void

deletenode

(listnode node)

點我跳轉題目鏈結

方法一:

//時間會慢

public listnode rotateright2

(listnode head,

int k)

else

k--;}

if(fast == slow)

return head;

while

(fast.next != null)

listnode tmp = slow.next;

slow.next = null;

fast.next = head;

head = tmp;

return head;

}

方法二:

//快

public listnode rotateright

(listnode head,

int k)

cur.next = head;

int step = count-k%count;

while

(step !=0)

cur = head;

while

(count !=1)

cur.next = null;

return head;

}

單鏈表的面試題

自定義標頭檔案部分 void deletenottail pnode pos 刪除乙個無頭單鏈表的非尾節點 void insertnothead pnode phead,pnode pos,datatype data 在無頭單鏈表的乙個非頭節點前插入乙個節點 void josephcircle pn...

單鏈表詳解以及單鏈表相關面試題總結

目錄 1.單鏈表的定義 2.獲取單鏈表的第i個元素 3.單鏈表的插入操作 4.單鏈表的刪除操作 5.單鏈表的整表刪除 6.判斷兩個單鏈表是否相交 7.如果兩個鍊錶相交,如何尋找相交的節點 8.判斷乙個單鏈表是否有環 9.如果乙個鍊錶有環,如何找到環的入口 10.找到單鏈表中的倒數第k個節點 11.兩...

C程式單鏈表面試題詳解

測試空間旗下大頭針 出品 最近c語言輔導過程中,發現有很多同學對於有關c語言鍊錶的問題一直不是很清楚,而在最近部分臨畢業學員應聘測試工作中又遇到單鏈表的問題而且反應不是很熟練這塊的內容。今天就談談有關但單鏈表的基本用法。鍊錶1。是由結構體和指標構成的。2。包括兩個部分乙個是資料域和指標域。3。鍊錶中...