wuyijia

导航

链表-代码随想录234. 回文链表、143. 重排链表

链表问题:双指针、迭代法、递归法(天然可用)、虚拟头节点

双链表 既可以向前查询也可以向后查询。

循环链表,顾名思义,就是链表首尾相连。循环链表可以用来解决约瑟夫环问题。

234. 回文链表

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 数组模拟
 7 class Solution:
 8     def isPalindrome(self, head: Optional[ListNode]) -> bool:
 9         list=[]
10         while head: 
11             list.append(head.val)
12             head=head.next
13         l,r=0, len(list)-1
14         while l<=r: 
15             if list[l]!=list[r]:
16                 return False
17             l+=1
18             r-=1
19         return True   
20       
21 #反转后半部分链表
22 class Solution:
23     def isPalindrome(self, head: Optional[ListNode]) -> bool:
24         fast = slow = head
25 
26         # find mid point which including (first) mid point into the first half linked list
27         while fast and fast.next:
28             fast = fast.next.next
29             slow = slow.next
30         node = None
31 
32         # reverse second half linked list
33         while slow:
34             # slow.next, slow, node = node, slow.next, slow
35             temp = slow.next
36             slow.next = node
37             node = slow
38             slow = temp
39 
40         # compare reversed and original half; must maintain reversed linked list is shorter than 1st half
41         while node:
42             if node.val != head.val:
43                 return False
44             node = node.next
45             head = head.next
46         return True

 143. 重排链表

 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution:
 7     def reorderList(self, head: Optional[ListNode]) -> None:
 8         """
 9         Do not return anything, modify head in-place instead.
10         """
11         d = collections.deque()
12         tmp = head
13         while tmp.next: # 链表除了首元素全部加入双向队列
14             d.append(tmp.next)
15             tmp = tmp.next
16         tmp = head
17         while len(d): # 一后一前加入链表
18             tmp.next = d.pop()
19             tmp = tmp.next
20             if len(d):
21                 tmp.next = d.popleft()
22                 tmp = tmp.next
23         tmp.next = None # 尾部置空
24 
25         return head
 1 # Definition for singly-linked list.
 2 # class ListNode:
 3 #     def __init__(self, val=0, next=None):
 4 #         self.val = val
 5 #         self.next = next
 6 class Solution:
 7     def reorderList(self, head: Optional[ListNode]) -> None:
 8         """
 9         Do not return anything, modify head in-place instead.
10         """
11         # d = collections.deque()
12         # tmp = head
13         # while tmp.next: # 链表除了首元素全部加入双向队列
14         #     d.append(tmp.next)
15         #     tmp = tmp.next
16         # tmp = head
17         # while len(d): # 一后一前加入链表
18         #     tmp.next = d.pop()
19         #     tmp = tmp.next
20         #     if len(d):
21         #         tmp.next = d.popleft()
22         #         tmp = tmp.next
23         # tmp.next = None # 尾部置空
24 
25         # return head
26         if head == None or head.next == None:
27             return True
28         slow, fast = head, head
29         while fast and fast.next:
30             slow = slow.next
31             fast = fast.next.next
32         right = slow.next # 分割右半边
33         slow.next = None # 切断
34         right = self.reverseList(right) #反转右半边
35         left = head
36         # 左半边一定比右半边长, 因此判断右半边即可
37         while right:
38             curLeft = left.next
39             left.next = right
40             left = curLeft
41 
42             curRight = right.next
43             right.next = left
44             right = curRight
45 
46         return head
47 
48 
49     def reverseList(self, head: ListNode) -> ListNode:
50         cur = head   
51         pre = None
52         while(cur!=None):
53             temp = cur.next # 保存一下cur的下一个节点
54             cur.next = pre # 反转
55             pre = cur
56             cur = temp
57         return pre

 

 

 

 
 

posted on 2023-08-29 08:24  小吴要努力  阅读(5)  评论(0编辑  收藏  举报