题目:给定单链表的头节点 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 class reverseList { public static void main(String[] args){ reverseList r = new reverseList(); ListNode node1 = new ListNode(5); ListNode node2 = new ListNode(6,node1); ListNode node3 = new ListNode(7,node2); r.fun(node3); } public ListNode fun(ListNode head){ ListNode preNode = null; ListNode curr = head; while(curr != null){ ListNode next = curr.next; curr.next = preNode; preNode = curr; curr = next; } return preNode; } }
标签:curr,val,反转,next,链表,new,ListNode From: https://www.cnblogs.com/99kol/p/16743486.html