單鏈表操作

2021-09-12 18:18:31 字數 3699 閱讀 2819

力扣206

# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

# 頭插入法

class solution:

def reverselist(self, head):

""":type head: listnode

:rtype: listnode

"""p = head

rev = none

q = none

while p:

q = p.next # 臨時儲存下乙個節點位置

p.next = rev

rev = p

p = q

return rev

使用快慢指標去判斷是否成環,快指標每次步長為2,慢指標每次步長為1,如果快指標能追上慢指標則說明有環,否則表示沒有環

力扣141

# definition for singly-linked list.

# class listnode(object):

# def __init__(self, x):

# self.val = x

# self.next = none

class solution(object):

def hascycle(self, head):

""":type head: listnode

:rtype: bool

"""fast = head

slow = head

while fast and fast.next:

slow = slow.next

fast = fast.next.next

if fast==slow:

return true

return false

3、兩個有序的鍊錶合併 

力扣21

# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

def mergetwolists(self, l1: listnode, l2: listnode) -> listnode:

head = listnode(0)

result = head # 臨時儲存頭指標的位置

while l1 and l2:

if l1.val <= l2.val:

head.next = l1

l1 = l1.next

else:

head.next = l2

l2 = l2.next

head = head.next

# 將剩餘的長鍊表對接上

if l1:

head.next = l1

else:

head.next = l2

return result.next

力扣19

例項:

給定乙個鍊錶:1->2->3->4->5, 和n = 2.

當刪除了倒數第二個節點後,鍊錶變為1->2->3->5

思路:

使用兩個節點即快慢指標,乙個是first乙個是slow。先讓first走n步,然後再讓first和second同時往前走,當first走到頭時,second即是倒數第n+1個節點了。

# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

def removenthfromend(self, head: listnode, n: int) -> listnode:

if head is none:

return none

result = listnode(0)

result.next = head

first = result

slow = result

while n>=0:

first = first.next

n -= 1

while first:

first = first.next

slow = slow.next

slow.next = slow.next.next

return result.next

力扣876

給定乙個帶有頭結點head的非空單鏈表,返回鍊錶的中間結點。

如果有兩個中間結點,則返回第二個中間結點。

思路一:

先遍歷一遍鍊錶得到鍊錶的長度,然後求出中間的位置,最後在遍歷到中間位置返回;

# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

def middlenode(self, head: listnode) -> listnode:

count = 0

p = head

while p:

p = p.next

count += 1

count = count//2

while count>0:

head = head.next

count -= 1

return head

思路二:

使用快慢指標,快指標步長為2,慢指標步長為1,當快指標遍歷完成時,慢指標正好是中間位置;

# definition for singly-linked list.

# class listnode:

# def __init__(self, x):

# self.val = x

# self.next = none

class solution:

def middlenode(self, head: listnode) -> listnode:

fast = slow = head

while fast and fast.next:

fast = fast.next.next

slow = slow.next

return slow

單鏈表操作

include include typedef struct node tag node 建立不帶頭結點的單鏈表 node createnode else p q scanf d n 函式體結束,q指標變數被釋放,不能通過head引數帶回到呼叫函式 要把head帶回到呼叫函式,要把它定義為指向指標的...

單鏈表操作

include stdio.h include malloc.h include define n 10 代表要處理的元素個數 可以誰使用者的意思修改 define ok 1 define overflow 0 typedef int elemtype typedef int status type...

單鏈表操作

這一次補上鍊表的注釋,是空閒的時候敲出來的,如果有錯,希望幫忙糾正 部分給出了詳細說明,這裡只選取了基本操作,因為更複雜的鍊錶操作太繁瑣,這裡就不寫了 如果有什麼不懂的地方,可以隨時詢問 include using namespace std typedef int elemtype struct ...